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

时间:2011-03-29 13:47:35

标签: java android image

我写了一些代码,可以让我在Android内部存储中的数据/数据中保存图片。现在我想知道是否有办法从内部存储中删除这些图片。

以下是我保存的内容:

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

我试过这个,但它没有从数据/数据中删除。关于我做错了什么的任何建议?

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

1 个答案:

答案 0 :(得分:1)

试试这个:

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