简体   繁体   中英

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(); 
} 

Try this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM