gpt4 book ai didi

安卓 : How to clear cache by program without affecting database?

转载 作者:行者123 更新时间:2023-11-30 03:25:06 25 4
gpt4 key购买 nike

 public void trimCache() {
try {
File dir = this.getCacheDir();
File appDir = new File(dir.getParent());
if (appDir != null && appDir.isDirectory()) {
new clearcache().execute(appDir);
}
} catch (Exception e) {
// TODO: handle exception
}
}
public class clearcache extends AsyncTask<File ,Void, Void>{

@Override
protected Void doInBackground(File... params) {
deleteDir(params[0]);
Log.d("clearcache", params[0].length()+"");
return null;
}
public boolean deleteDir(File dir) {
if (dir != null && 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();
}
}

我正在使用上面的代码来清除我的缓存内存,但这也会删除我的数据库更新。如何在不影响数据库更新的情况下只清除缓存(图像缓存)?

最佳答案

我们可以避免清除数据库缓存。这是我对上述问题的回答

 public boolean deleteDir(File dir) {
if (dir != null && dir.isDirectory()) {
String[] children = dir.list();
for (int i = 0; i < children.length; i++) {
if(!dir.getAbsolutePath().contains("/yourDataBaseName")){
{
//clear all files except your database directory
boolean success = deleteDir(new File(dir, children[i]));
if (!success) {
return false;
}
}
}
}
// The directory is now empty so delete it
return dir.delete();

关于安卓 : How to clear cache by program without affecting database?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18356825/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com