开发者

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) curren开发者_StackOverflowt);
      }

      //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();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜