【发布时间】:2013-03-08 10:13:33
【问题描述】:
我想标题说明了一切。我试过了:
imageLoader.getMemoryCache().get(key);
以图片uri为key,但总是返回null
虽然我在配置中启用了缓存。
【问题讨论】:
标签: android universal-image-loader
我想标题说明了一切。我试过了:
imageLoader.getMemoryCache().get(key);
以图片uri为key,但总是返回null
虽然我在配置中启用了缓存。
【问题讨论】:
标签: android universal-image-loader
使用MemoryCacheUtils。
MemoryCacheUtils.findCachedBitmapsForImageUri(imageUri, ImageLoader.getInstance().getMemoryCache());
内存缓存可以包含一个图像的多个位图(不同大小)。所以内存缓存使用特殊键,而不是图像 url。
【讨论】:
应该是MemoryCacheUtils,所以应该使用
MemoryCacheUtils.findCachedBitmapsForImageUri(imageUri, ImageLoader.getInstance().getMemoryCache());
【讨论】:
磁盘缓存使用下面的代码
public static boolean isDiskCache(String url) {
File file = DiskCacheUtils.findInCache(url, ImageLoader.getInstance().getDiskCache());
return file != null;
}
【讨论】:
有时,在使用通用图像加载器库时,加载器需要一段时间来验证远程图像是否已加载到缓存中。要直接加载缓存文件,可以使用以下方法检查远程文件的本地副本是否已经生成:
File file = imageLoader.getDiscCache().get(url);
if (!file.exists()) {
DisplayImageOptions options = new DisplayImageOptions.Builder()
.cacheOnDisc()
.build();
imageLoader.displayImage(url, imageView, options);
}
else {
imageView.setImageURI(Uri.parse(file.getAbsolutePath()));
}
【讨论】:
我认为您可以像这样在实用程序类中创建一个简单的方法:
public static boolean isImageAvailableInCache(String imageUrl){
MemoryCache memoryCache = ImageLoader.getInstance().getMemoryCache();
if(memoryCache!=null) {
for (String key : memoryCache.keys()) {
if (key.startsWith(imageUrl)) {
return true;
}
}
}
return false;
}
并像这样使用它:
if(Utils.isImageAvailableInCache(imageUrl)){
//
}
【讨论】: