And*_*oid 1 size android caching get
我在测试应用程序中使用fedor的延迟加载列表实现,通过单击该按钮即可清除缓存。如何获取列表视图中已加载图像的缓存大小并以编程方式清除缓存?
这是用于保存缓存图像的代码:
public ImageLoader(Context context){
//Make the background thead low priority. This way it will not affect the UI performance.
photoLoaderThread.setPriority(Thread.NORM_PRIORITY-1);
mAssetManager = context.getAssets();
//Find the dir to save cached images
if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED))
cacheDir = new File(android.os.Environment.getExternalStorageDirectory(),"LazyList");
else
cacheDir = context.getCacheDir();
if(!cacheDir.exists())
cacheDir.mkdirs();
}
Run Code Online (Sandbox Code Playgroud)
编辑:
因此,基本上我将这段代码添加到了clearCache()方法中,但是在滚动时仍然看不到图像再次开始加载。
public void clearCache() {
//clear memory cache
long size=0;
cache.clear();
//clear SD cache
File[] files = cacheDir.listFiles();
for (File f:files) {
size = size+f.length();
if(size >= 200)
f.delete();
}
}
Run Code Online (Sandbox Code Playgroud)
要查找缓存目录的大小,请使用以下代码。
public void clearCache() {
//clear memory cache
long size = 0;
cache.clear();
//clear SD cache
File[] files = cacheDir.listFiles();
for (File f:files) {
size = size+f.length();
f.delete();
}
}
Run Code Online (Sandbox Code Playgroud)
这将返回字节数。
这对我来说更准确:
private void initializeCache() {
long size = 0;
size += getDirSize(this.getCacheDir());
size += getDirSize(this.getExternalCacheDir());
}
public long getDirSize(File dir){
long size = 0;
for (File file : dir.listFiles()) {
if (file != null && file.isDirectory()) {
size += getDirSize(file);
} else if (file != null && file.isFile()) {
size += file.length();
}
}
return size;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9321 次 |
| 最近记录: |