【问题标题】:How to get cache size in Android如何在 Android 中获取缓存大小
【发布时间】:2011-08-12 06:48:13
【问题描述】:

我在我的测试应用程序中使用了 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();
}

编辑:

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

【问题讨论】:

    标签: android caching get size


    【解决方案1】:

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

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

    这将返回字节数。

    【讨论】:

    • 我只是用我现在使用的代码编辑我的问题,但向下滚动后仍然看不到图像加载。
    • 你在哪里调用清除缓存?以及为什么要执行此代码 if(size>=200) f.delete();
    • 我在我的主要活动中这样称呼它:adapter.imageLoader.clearCache();适配器.notifyDataSetChanged(); .我放了 IF,因为我希望它在缓存大小达到 200kb 时删除它。我这样做对吗?
    • 好的,当它大约 200kb 时,我如何调用那段代码来清除缓存?
    • 使用上面的代码。我认为 tat 可以解决你的问题。现在我已经编辑了那个代码?
    【解决方案2】:

    这对我来说更准确:

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

    【讨论】:

      【解决方案3】:

      在 Kotlin 中,您可以使用:

      context.cacheDir.walkBottomUp().fold(0L, { acc, file -> acc + file.length() })
      

      或将其定义为扩展函数

      fun File.calculateSizeRecursively(): Long {
          return walkBottomUp().fold(0L, { acc, file -> acc + file.length() })
      }
      
      
      // usage
      val size = context.cacheDir.calculateSizeRecursively()
      
      

      【讨论】:

        【解决方案4】:

        ...要清除缓存,只需 delete the directory 并重新创建一个空缓存。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2020-08-21
          • 2018-02-05
          • 2020-10-18
          • 2016-12-07
          • 2020-01-09
          • 2018-02-16
          • 1970-01-01
          相关资源
          最近更新 更多