如何清除缓存的Android [英] How to clear cache Android
本文介绍了如何清除缓存的Android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要找到一种方法如何清除这cache.Basically我的应用程序商店,我使用费多尔的(<一个数据href="http://stackoverflow.com/questions/541966/android-how-do-i-do-a-lazy-load-of-images-in-listview/3068012#3068012">Android - 如何做我做的ListView 图片)懒列表实现的延迟加载,我想自动清除缓存时,我有例如100张图像loaded.Any想法如何做到这一点
编辑: code:
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.main);
名单=(ListView控件)findViewById(R.id.list);
适配器=新LazyAdapter(这一点,mStrings);
list.setAdapter(适配器);
deleteCache(本);
adapter.notifyDataSetChanged();
}
公共静态无效deleteCache(上下文的背景下){
尝试 {
文件DIR = context.getCacheDir();
如果(DIR = NULL和放大器;!&安培; dir.isDirectory()){
deleteDir(DIR);
}
}赶上(例外五){}
}
公共静态布尔deleteDir(文件目录){
如果(DIR = NULL和放大器;!&安培; dir.isDirectory()){
的String []孩子= dir.list();
的for(int i = 0; I&LT; children.length;我++){
布尔成功= deleteDir(新建文件(目录,孩子们[I]));
如果(!成功){
返回false;
}
}
}
返回dir.delete();
}
解决方案
这将删除缓存
公共静态无效deleteCache(上下文的背景下){
尝试 {
文件DIR = context.getCacheDir();
如果(DIR = NULL和放大器;!&安培; dir.isDirectory()){
deleteDir(DIR);
}
}赶上(例外五){}
}
公共静态布尔deleteDir(文件目录){
如果(DIR = NULL和放大器;!&安培; dir.isDirectory()){
的String []孩子= dir.list();
的for(int i = 0; I&LT; children.length;我++){
布尔成功= deleteDir(新建文件(目录,孩子们[I]));
如果(!成功){
返回false;
}
}
}
返回dir.delete();
}
I need to find a way how to clear the data which my application stores in cache.Basically I am using Fedor's ( Android - How do I do a lazy load of images in ListView ) lazy list implementation and I want to clear the cache automatically when I have for example 100 images loaded.Any ideas how to do that?
EDIT: Code :
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();
}
解决方案
this will delete cache
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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文