Search code examples
androidandroid-contentprovider

Android clearing app cache clears provider also


I used following code to delete my app cache.

public void clearApplicationData() {

        File cacheDirectory = getCacheDir();
        File applicationDirectory = new File(cacheDirectory.getParent());
        if (applicationDirectory.exists()) {
            String[] fileNames = applicationDirectory.list();
            for (String fileName : fileNames) {
                if (!fileName.equals("lib")) {
                    deleteFile(new File(applicationDirectory, fileName));
                }
            }
        }
    }

    public static boolean deleteFile(File file) {
        boolean deletedAll = true;
        if (file != null) {
            if (file.isDirectory()) {
                String[] children = file.list();
                for (int i = 0; i < children.length; i++) {
                    deletedAll = deleteFile(new File(file, children[i])) && deletedAll;
                }
            } else {
                deletedAll = file.delete();
            }
        }
        return deletedAll;
    }

Once I delete the code means it deletes the provider which I declared in manifest. Is there any way to clear cache without deleting content provider?


Solution

  • You can avoid this by not deleting database folder

    if (!fileName.equals("lib")&&!fileName.equals("files")&&!fileName.equals("database")) {
                        deleteFile(new File(applicationDirectory, fileName));
                    }