2013-03-29 50 views
6

我無法從SD卡刪除文件。Android文件刪除不起作用

File toDelete = new File(fname); 
boolean result=toDelete.delete(); 

結果是假的。同一個文件的讀寫操作在同一個應用程序中運行。 沒有打開的流。沒有例外提出。 我試圖使其在剛刪除之前可寫入

toDelete.setWritable(true); 

沒有效果。 系統如何可以寫入和讀取,但不能刪除相同的文件?

+1

請認真添加一些關於如何建立文件路徑的代碼 – Trinimon

+0

我可以讀寫這個文件,沒有路徑或權限問題,系統只是拒絕刪除它 – Catherine

+0

我想你是以編程方式創建文件或運行??我對嗎? –

回答

3

使用下面的代碼它可以幫助你。

  File fdelete = new File(file_dj_path); 
     if (fdelete.exists()) { 
      if (fdelete.delete()) { 
       System.out.println("file Deleted :" + file_dj_path); 
      } else { 
       System.out.println("file not Deleted :" + file_dj_path); 
      } 
     } 

刷新畫廊在此之後刪除圖像

sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, 
Uri.parse("file://" + Environment.getExternalStorageDirectory()))); 

檢查:https://stackoverflow.com/a/10716773/1168654

+0

嗨!謝謝你的回答,但文件並沒有一直刪除,它留在SD卡上,因爲我可以看穿./adb外殼 – Catherine

+0

你能告訴我們你的問題更多關於文件路徑的代碼..? –

1

嘗試這樣做

File fileToDelete = new File(YourPath); 
boolean deleted = fileToDelete.delete(); 

再次重新掛載卡,並檢查

sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, 
        Uri.parse("file://" 
           + Environment.getExternalStorageDirectory()))); 
+0

這種方式不起作用 – Catherine