簡體   English   中英

如何在Android中以編程方式刪除圖片?

[英]How can I delete picture programmatically in Android?

我編寫了一些代碼,使我可以將圖片保存在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(); 
} 

嘗試這個:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM