我使用以下代码删除了我的应用缓存。
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;
}
删除代码后,意味着删除我在清单中声明的提供程序。有没有办法在不删除内容提供商的情况下清除缓存?
答案 0 :(得分:1)
您可以通过不删除数据库文件夹来避免这种情况
if (!fileName.equals("lib")&&!fileName.equals("files")&&!fileName.equals("database")) {
deleteFile(new File(applicationDirectory, fileName));
}