如何在Android中获取缓存大小 [英] How to get cache size in Android

查看:43
本文介绍了如何在Android中获取缓存大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm using fedor's lazy loading list implementation in my test application where I can clear the cache with a single button click. How can I get the cache size of the loaded images in the listview and clear the cache programmatically?

这是保存缓存图像的代码:

Here is the code for saving the cached images:

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() 方法中添加了这段代码,但是当我滚动时仍然看不到图像再次开始加载.

So basically I added this piece of code in clearCache(), method, but I still cannot see the images start loading again when I'm scrolling.

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

推荐答案

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

To find the size of the cache directory use the codebelow.

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中获取缓存大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆