我正在使用Trey Robinson 的 BitmapLRUCache在我的 Android 应用程序中进行图像缓存。它是 Volley 的 LRU 缓存实现,因为它本身不提供任何图像缓存。
尽管它确实使用DiskBasedCache来缓存 HTTP 请求。现在遇到问题,当 DiskBasedCache 尝试获取或删除缓存条目时,我反复收到 FileNotFoundExceptions。
下面的示例日志。
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
当我使用 BitmapLRUcache(见下文)初始化 ImageLoader 时,为什么 DiskBasedCache 处理图像缓存?
ImageLoader imageLoader = new ImageLoader(Volley.newRequestQueue(this), new BitmapLruCache());
下面是我用于缓存的代码。
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;
}
}