简体   繁体   English

检查映像是否在缓存中-Universal Image Loader

[英]Check if Image is in Cache - Universal Image Loader

I guess the title says it all. 我想标题说明了一切。 I tried: 我试过了:

imageLoader.getMemoryCache().get(key); 

with the image uri as key, but it always return null 以图片uri作为键,但它始终返回null

although I enabled caching in the config. 虽然我在配置中启用了缓存。

Use MemoryCacheUtils . 使用MemoryCacheUtils

MemoryCacheUtils.findCachedBitmapsForImageUri(imageUri, ImageLoader.getInstance().getMemoryCache());

Memory cache can contain several bitmaps (diffenrent sizes) for one image. 内存缓存可以为一个图像包含几个位图(不同大小)。 So memory cache use special keys, not image urls. 因此,内存缓存使用特殊键,而不是图像URL。

它应该是MemoryCacheUtil S,所以你应该使用

MemoryCacheUtils.findCachedBitmapsForImageUri(imageUri, ImageLoader.getInstance().getMemoryCache());

For disk cache use below code 对于磁盘缓存,请使用以下代码

public static boolean isDiskCache(String url) {
        File file = DiskCacheUtils.findInCache(url, ImageLoader.getInstance().getDiskCache());
        return file != null;
}

Sometimes, when using the Universal Image Loader library, there comes a time where the loader takes a while to verify whether the remote image has been already loaded in your cache. 有时,在使用Universal Image Loader库时,有时加载器会花一些时间来验证是否已将远程图像加载到缓存中。 To load the cache file directly, you can use the following method to check whether a local copy of the remote file has already been made: 要直接加载缓存文件,可以使用以下方法检查是否已创建远程文件的本地副本:

    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()));  
 }  

check it here enter link description here 在这里检查,在这里输入链接描述

I think you can create a simple method in your utility class like this: 我认为您可以在实用程序类中创建一个简单的方法,如下所示:

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;
}

and Use it like: 并像这样使用它:

   if(Utils.isImageAvailableInCache(imageUrl)){
//
      }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM