2012-12-04 68 views
0

我的應用程序我解析一個html文件,並存儲在應用程序本地文件directory.this我在做onCreate()。現在他們是一個按鈕「打開」,當我點擊「打開」按鈕時,我通過將路徑作爲文件目錄來顯示android選擇器對話框。在哪裏刪除在android中本地存儲的文件?

然後在onDestroy()我刪除存儲在文件目錄中的所有文件。這正在工作到4.0。

但在4.1,因爲一旦我們打開選擇器對話框,它立即調用onDestroy()。到另一個應用程序(例如:Html viewer)嘗試打開文件時,文件將不可用,因此它顯示的文件未找到。是否刪除本地文件?

回答

0

試試這個代碼

//Deleting the temperary folder and the file created in the sdcard 
public static boolean deleteDir(File dir) 
{ 
    if (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; 
      } 
     } 
    } 

    // The directory is now empty so delete it 
    return dir.delete(); 
} 

將這個代碼onDestroy()

File checkFile = new File(Environment.getExternalStorageDirectory(),"/FolderName/"); 
//getting the control of sdcard files 
deleteDir(checkFile); 
+0

裏感謝u代表reply.i我能夠刪除文件,但如果我需要刪除的文件(現在我在onDestroy()中刪除,但這對我沒有幫助)。在殺死應用程序之前,有沒有任何方法? –

+0

@ashokreddy您有多少活動? –

+0

五大活動。 –