gpt4 book ai didi

android - 如何在 Android 中获取缓存大小

转载 作者:行者123 更新时间:2023-11-29 14:29:07 26 4
gpt4 key购买 nike

我在我的测试应用程序中使用了 fedor 的延迟加载列表实现,我可以通过单击一个按钮来清除缓存。如何获取 ListView 中加载图像的缓存大小并以编程方式清除缓存?

下面是保存缓存图片的代码:

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();
}

编辑:

所以基本上我在 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();
}
}

最佳答案

要查找缓存目录的大小,请使用下面的代码。

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();
}
}

这将返回字节数。

关于android - 如何在 Android 中获取缓存大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7036538/

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