如何获得在Android的高速缓存大小 [英] How to get cache size in Android

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

问题描述

我用我的测试应用程序的费多尔的延迟加载列表实现在那里我可以清除与一个单一的按一下按钮缓存。我怎样才能加载的图像列表视图的高速缓存大小和编程方式清除缓存?

下面是code保存缓存的图片:

 公共ImageLoader的(上下文的背景下){
    //使背景THEAD低优先级。这样也不会影响用户界面的性能。
    photoLoaderThread.setPriority(Thread.NORM_PRIORITY-1);
    mAssetManager = context.getAssets();

    //查找目录保存缓存图像
    如果(android.os.Environment.getExternalStorageState()。等于(android.os.Environment.MEDIA_MOUNTED))
        cacheDir =新的文件(android.os.Environment.getExternalStorageDirectory(),LazyList);
    其他
        cacheDir = context.getCacheDir();
    如果(!cacheDir.exists())
        cacheDir.mkdirs();
}
 

编辑:

所以基本上我说这块code在clearCache()方法,但我仍然无法看到图像开始加载一次,当我滚动。

 公共无效clearCache(){
    //清除内存高速缓存

    长大小= 0;
    cache.clear();

    //明确的SD高速缓存
    文件[]文件= cacheDir.listFiles();
    对于(F文件:文件){
        大小= + f.length();
        如果(大小与GT; = 200)
            f.delete();
    }
}
 

解决方案

要找到缓存目录的大小,使用codebelow。

 公共无效clearCache(){
    //清除内存高速缓存

    长大小= 0;
    cache.clear();

    //明确的SD高速缓存
    文件[]文件= cacheDir.listFiles();
    对于(F文件:文件){
        大小= + f.length();
        f.delete();
    }
}
 

此将返回的字节数。

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

EDIT:

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

This will return the number of bytes.

这篇关于如何获得在Android的高速缓存大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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