Mar*_*kus 6 android caching bitmap android-volley
我正在尝试显示包含大量(远程)图像的列表视图.我正试图用凌空来完成任务.
凌空有点奏效,但还不够好.在ImageLoader.get中,volley有以下代码:
final String cacheKey = getCacheKey(requestUrl, maxWidth, maxHeight);
// Try to look up the request in the cache of remote images.
Bitmap cachedBitmap = mCache.getBitmap(cacheKey);
if (cachedBitmap != null) {
// Return the cached bitmap.
ImageContainer container = new ImageContainer(cachedBitmap, requestUrl, null, null);
imageListener.onResponse(container, true);
return container;
}
Run Code Online (Sandbox Code Playgroud)
但是,getCacheKey会生成如下所示的键:
/**
* Creates a cache key for use with the L1 cache.
* @param url The URL of the request.
* @param maxWidth The max-width of the output.
* @param maxHeight The max-height of the output.
*/
private static String getCacheKey(String url, int maxWidth, int maxHeight) {
return new StringBuilder(url.length() + 12).append("#W").append(maxWidth)
.append("#H").append(maxHeight).append(url).toString();
}
Run Code Online (Sandbox Code Playgroud)
即它会向键附加一些"元数据",如宽度和高度.
此密钥永远不会产生命中,如果图像不在L1缓存中,则会在线获取.当在线获取图像时,它将保存在磁盘缓存中,但Volley会将URL(仅限URL)保存为密钥.
这是预期的行为吗?我错过了什么吗?
您没有获得任何匹配的原因是因为Volley中用于磁盘缓存的默认行为取决于您请求的元素的HTTP标头(在您的情况下是图像).
Volley的工作方式是:
ImageLoader检查L1缓存中的映像(由您提供给ImageLoader其构造函数的内存缓存).如果可用则返回图像.RequestQueue.它检查映像的L2(磁盘缓存).如果您希望默认设置起作用,则图像必须具有Cache-Control标题,如 max-age=???问号标记表示从下载时起的足够秒数.
如果你想改变默认行为,我不确定,但我认为你必须稍微编辑一下代码.
看看CacheDispatcherVolley来源的课程.