2013-02-06 68 views
5

enter image description here我實現了一個在外部存儲中生成和創建文件的應用程序。你們能幫我解決嗎?Android:如何從外部存儲中刪除文件?

謝謝 錢德拉

編輯-1:我添加以下代碼,還是我收到了同樣的問題,請看看我下面的代碼,

String fullPath = "/mnt/sdcard/"; 
       System.out.println(fullPath); 
       try 
       { 
        File file = new File(fullPath, "audio.mp3"); 
        if(file.exists()) 
        { 

         boolean result = file.delete(); 
         System.out.println("Application able to delete the file and result is: " + result); 
         // file.delete(); 
        } 
        else 
        { 
         System.out.println("Application doesn't able to delete the file"); 
        } 
       } 
       catch (Exception e) 
       { 
        Log.e("App", "Exception while deleting file " + e.getMessage()); 
       } 

在LogCat我得到應用程序能夠刪除該文件,結果爲:false。執行此操作後,我附加了我的屏幕截圖。請看看這個。並建議我。

回答

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

其中selectedFilePath是要刪除的文件的路徑 - 例如:

/sdcard/YourCustomDirectory/ExampleFile.mp3 
+0

如果你的文件是外部移動存儲(SD卡)內,你應該使用DocumentFile和存儲訪問架構而不是爲版本> =奇巧 – HendraWD

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

路徑會是這樣的:/mnt/Pictures/SampleImage.png

1
public void deleteOnExit() 

時間表此文件時,虛擬機正常終止被自動刪除。

請注意,在Android上,應用程序生命週期不包括虛擬機終止,因此調用此方法不會確保刪除文件。相反,您應該使用最合適的:

使用finally子句手動調用delete()。 維護您自己的一組文件以進行刪除,並在應用程序生命週期的適當時刻對其進行處理。 只要所有讀者和作者打開文件,就可以使用刪除文件的Unix技巧。沒有新的讀者/作者能夠訪問該文件,但是所有現有的讀者/作者將仍然有權訪問,直到最後一個關閉該文件。

1

它非常簡單:

File file = new File(YOUR_IMAGE_PATH).delete(); 
if(file.exists()) 
    file.delete(); 

希望它會幫助你。

2

這是另一種方式只是通過你的目錄名來刪除它的內容

"/sdcard/abc/yourdata"

public void deleteFiles(String path) {  
    File file = new File(path); 

    if (file.exists()) { 
     String deleteCmd = "rm -r " + path; 
     Runtime runtime = Runtime.getRuntime(); 
     try { 
      runtime.exec(deleteCmd); 
     } catch (IOException e) { 

     } 
    } 

} 
3

請嘗試以下代碼以刪除自動創建當你不知道它存儲在哪裏:

public void clearApplicationData() { 
    File cache = getCacheDir(); 
    File appDir = new File(cache.getParent()); 
    if (appDir.exists()) { 
     String[] children = appDir.list(); 
     for (String s : children) { 
      if (!s.equals("lib")) { 
       deleteDir(new File(appDir, s)); 
       Log.i("TAG", 
         "**************** File /data/data/APP_PACKAGE/" + s 
           + " DELETED *******************"); 
      } 
     } 
    } 
} 

public static boolean deleteDir(File dir) { 
    if (dir != null && dir.isDirectory()) { 
     String[] children = dir.list(); 
     for (int i = 0; i < children.length; i++) { 
      boolean success = deleteDir(new File(dir, children[i])); 
      if (!success) { 
       return false; 
      } 
     } 
    } 

    return dir.delete(); 
} 
1

試試這個,它會從外部存儲中刪除文件。

public void deleteFromExternalStorage(String fileName) { 
    String fullPath = "/mnt/sdcard/"; 
    try 
    { 
     File file = new File(fullPath, fileName); 
     if(file.exists()) 
      file.delete(); 
    } 
    catch (Exception e) 
    { 
     Log.e("App", "Exception while deleting file " + e.getMessage()); 
    } 
} 
+0

我用上面的代碼,並通過使用調試模式下測試,其實調試器去通過file.delete();.之後,我打開DDMS並檢查文件。不幸的是該文件仍然出現。你能指導我嗎?我如何鍛鍊? – ChandraSekhar

+0

您能否給出您存儲文件的路徑並且想要刪除.. –

+0

路徑是mnt/sdcard/audio.mp3 – ChandraSekhar

3

我想你忘了把寫權限放在你的AndroidManifest.xml文件中,這就是爲什麼delete()總是返回false的原因。

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
相關問題