我在我的Android应用程序中使用BitmapLRUCache by Trey Robinson进行图像缓存。这是Volley的LRU缓存实现,因为它本身不提供任何图像缓存。Volley DiskBasedCache抛出FileNotFoundException
尽管它确实使用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
为什么DiskBasedCache处理图像缓存,当我用初始化BitmapLRUcache(见下文)ImageLoader的?
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;
}
}
发布您的缓存代码 – mmlooloo 2014-10-07 18:11:42
我现在添加了它。 – Vibin 2014-10-08 02:58:06