在哪里删除本地存储在android中的文件?

时间:2012-12-04 10:57:30

标签: android file

我的应用程序我正在解析一个html文件并存储在app本地文件目录中。这是我在onCreate()中进行的。现在他们是一个“打开”按钮,当我点击“打开”按钮时,我通过将路径作为文件目录显示android选择器对话框。

然后在onDestroy()中我删除了存储在files目录中的所有文件。这个工作到4.0。

但在4.1中,因为一旦打开选择器对话框,它立即调用onDestroy()。当另一个应用程序(例如:Html查看器)尝试打开文件时,文件将不可用。所以它显示的文件未找到。所以删除本地文件的位置?

1 个答案:

答案 0 :(得分: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);