Check if Image is in Cache - Universal Image Loade

2020-06-02 10:02发布

I guess the title says it all. I tried:

imageLoader.getMemoryCache().get(key); 

with the image uri as key, but it always return null

although I enabled caching in the config.

5条回答
ゆ 、 Hurt°
2楼-- · 2020-06-02 10:20

Use 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.

查看更多
劫难
3楼-- · 2020-06-02 10:26

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)){
//
      }
查看更多
叛逆
4楼-- · 2020-06-02 10:39

It should be MemoryCacheUtils, so you should use

MemoryCacheUtils.findCachedBitmapsForImageUri(imageUri, ImageLoader.getInstance().getMemoryCache());
查看更多
Summer. ? 凉城
5楼-- · 2020-06-02 10:41

For disk cache use below code

public static boolean isDiskCache(String url) {
        File file = DiskCacheUtils.findInCache(url, ImageLoader.getInstance().getDiskCache());
        return file != null;
}
查看更多
相关推荐>>
6楼-- · 2020-06-02 10:45

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. 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 hereenter link description here

查看更多
登录 后发表回答