user661135
user661135

Reputation: 93

How can I delete picture programmatically in Android?

I wrote some code that lets me save pictures in my data/data in Android internal storage. Now I would like to know if there is a way to delete those pictures from internal storage.

Here is what I have for saving:

public boolean saveImg( String showId ) {
    try {
      URL url = new URL(getImgUrl( showId ));
      File file = new File(showId + ".jpg");

      /* Open a connection to that URL. */
      URLConnection ucon = url.openConnection();


      //Define InputStreams to read from the URLConnection.

      InputStream is = ucon.getInputStream();
      BufferedInputStream bis = new BufferedInputStream(is);


     //Read bytes to the Buffer until there is nothing more to read(-1).

      ByteArrayBuffer baf = new ByteArrayBuffer(50);
      int current = 0;
      while ((current = bis.read()) != -1) {
              baf.append((byte) current);
      }

      //Convert the Bytes read to a String.
      FileOutputStream fos = new FileOutputStream(PATH+file);
      fos.write(baf.toByteArray());
      fos.close();

      return true;
    } catch (IOException e) {
      return false;
    }
}

I tried this but it doesn't delete from data/data. Any suggestions as to what I'm doing wrong?

public void DeleteImg(String showId) { 
File file = new File( PATH + showId +".jpg" );
 file.delete(); 
} 

Upvotes: 1

Views: 5681

Answers (1)

Mark Mooibroek
Mark Mooibroek

Reputation: 7706

Try this:

File file = new File(selectedFilePath);
boolean deleted = file.delete();

Upvotes: 1

Related Questions