Volley DiskBasedCache throws FileNotFoundException

拜拜、爱过 提交于 2019-12-11 14:25:49

问题


I'm using BitmapLRUCache by Trey Robinson for image caching in my Android app. It's an implementation of LRU cache for Volley as it doesn't provide any image caching by itself.

Though it does use DiskBasedCache for caching HTTP requests. Now coming to the problem, I get FileNotFoundExceptions repeatedly when DiskBasedCache tries to get or remove cache entries.

Sample log below.

 23833                 Volley  D  [47291] DiskBasedCache.remove: Could not delete cache entry for key=http://a2.mzstatic.com/us/r30/Music1/v4/69/66/0b/69660b50-7771-a43a-919f-26d8b6ae37aa/UMG_cvrart_00602537957941_01_RGB72_1500x1500_14UMGIM31675.400x400-75.jpg, filename=1509125231-2004731303
 23833                 Volley  D  [47291] DiskBasedCache.get: /data/data/com.vibin.billy.debug/cache/volley/6408634861932551223: java.io.FileNotFoundException: /data/data/com.vibin.billy.debug/cache/volley/6408634861932551223: open failed: ENOENT (No such file or directory)
 23833                 Volley  D  [47291] DiskBasedCache.remove: Could not delete cache entry for key=http://a2.mzstatic.com/us/r30/Music4/v4/99/f7/ac/99f7ac13-0dd6-8841-96e0-2a1c18041d84/UMG_cvrart_00602537854097_01_RGB72_1800x1800_14UMGIM03851.400x400-75.jpg, filename=6408634861932551223

Why is DiskBasedCache handling image caching when I'm initializing the ImageLoader with BitmapLRUcache (see below)?

ImageLoader imageLoader = new ImageLoader(Volley.newRequestQueue(this), new BitmapLruCache());

Below is the code I'm using for caching.

package com.vibin.billy;

import android.graphics.Bitmap;
import android.support.v4.util.LruCache;
import android.util.Log;

import com.android.volley.toolbox.DiskBasedCache;
import com.android.volley.toolbox.ImageLoader;

/**
 * Basic LRU Memory cache.
 *
 * @author Trey Robinson
 */
public class BitmapLruCache
        extends LruCache<String, Bitmap>
        implements ImageLoader.ImageCache {

    private static final String TAG = BitmapLruCache.class.getSimpleName();

    public BitmapLruCache() {
        this(getDefaultLruCacheSize());
    }

    public BitmapLruCache(int sizeInKiloBytes) {
        super(sizeInKiloBytes);
    }


    @Override
    protected int sizeOf(String key, Bitmap value) {
        return value.getRowBytes() * value.getHeight() / 1024;
    }

    @Override
    public Bitmap getBitmap(String url) {
        //Log.d(TAG, "Grab "+url);
        return get(url);
    }

    @Override
    public void putBitmap(String url, Bitmap bitmap) {
         //Log.d(TAG, "Put "+url);
         put(url, bitmap);
    }

    public static int getDefaultLruCacheSize() {
        final int maxMemory =
                (int) (Runtime.getRuntime().maxMemory() / 1024);
        final int cacheSize = maxMemory / 8;

        Log.d(TAG, "cachesize is " + cacheSize);

        Log.d(TAG,cacheSize+" is cache Size");

        return cacheSize;
    }
}

回答1:


Why is DiskBasedCache handling image caching when I'm initializing the ImageLoader with BitmapLRUcache (see below)?

for image caching volley use 2 level cache mechanism, that means one level is in your RAM BitmapLRUcache and another one is on your Disk DiskBasedCache. why? because of reading and writing images from disk takes longer time than just simply reading and writing some Strings and gives poor performance. so at the first time when you request Volley to download your images Volley first looks at the cache level one, if your images are not there Volley looks at cache level 2 and if your images are not there Volley sends your download request to the server.

I get FileNotFoundExceptions repeatedly when DiskBasedCache tries to get or remove cache entries.

because your DiskBasedCache size is limited(by default is 5MB) and also it is LRU, which means if Volley wants to store an image on the DiskBasedCache and it dose not have any space, it is going to delete some of the old entries that it holds and dose not references recently.(LRU=Least Recently Used) So the remove function is called.



来源:https://stackoverflow.com/questions/26193501/volley-diskbasedcache-throws-filenotfoundexception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!