在哪里删除本地存储在安卓中的文件



我的应用程序我正在解析一个html文件并存储在应用程序本地文件目录中。现在有一个按钮"打开",当我单击"打开"按钮时,我通过将路径作为文件目录来显示android选择器对话框。

然后在onDestroy()中,我正在删除存储在文件目录中的所有文件。

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

试试这段代码

//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);

最新更新