2011-03-29 83 views
0

我写了一些代码,可以让我在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(); 
} 

回答

1

试试这个:

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

嗯,我试过了。模拟器运行时不会崩溃。该节目从我的列表视图中删除,但当看看数据/数据时,我仍然看到文件::: public void DeleteImg(String showId){ File file = new File(PATH + showId +“。jpg”); file.delete(); } – user661135 2011-03-29 18:51:37

相关问题