gpt4 book ai didi

android - 如何清除缓存Android

转载 作者:IT老高 更新时间:2023-10-28 23:29:33 30 4
gpt4 key购买 nike

我需要找到一种方法来清除我的应用程序存储在缓存中的数据。基本上我正在使用 Fedor 的 (Lazy load of images in ListView) 惰性列表实现,我想在加载了例如 100 个图像时自动清除缓存.任何想法如何做到这一点?

编辑:代码:

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

list=(ListView)findViewById(R.id.list);
adapter=new LazyAdapter(this, mStrings);
list.setAdapter(adapter);

deleteCache(this);
adapter.notifyDataSetChanged();


}

public static void deleteCache(Context context) {
try {
File dir = context.getCacheDir();
if (dir != null && dir.isDirectory()) {
deleteDir(dir);
}
} catch (Exception e) {}
}

public static 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;
}
}
}
return dir.delete();
}

最佳答案

这将删除缓存

public static void deleteCache(Context context) {
try {
File dir = context.getCacheDir();
if (dir != null && dir.isDirectory()) {
deleteDir(dir);
}
} catch (Exception e) {}
}

public static 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;
}
}
}
return dir.delete();
}

关于android - 如何清除缓存Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6898090/

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