简体   繁体   English

如何在Android中以编程方式删除图片?

[英]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. 我编写了一些代码,使我可以将图片保存在Android内部存储器中的数据中。 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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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