Check if Image is in Cache - Universal Image Loader
Asked Answered
D

5

13

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.

Donley answered 19/3, 2013 at 16:5 Comment(0)
T
32

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.

Tannertannery answered 19/3, 2013 at 16:46 Comment(4)
@NOSTRA this is not working for me. If I use: MemoryCacheUtil.findCachedBitmapsForImageUri(url, ImageLoader.getInstance()); it asks me for casting, so... is this correct? MemoryCacheUtil.findCachedBitmapsForImageUri(url, (MemoryCacheAware<String, Bitmap>) ImageLoader.getInstance());Gwenny
@NOSTRA, why using this code MemoryCacheUtil.findCachedBitmapsForImageUri(imageUri, ImageLoader.getInstance().getMemoryCache()); returns an arraylist of bitmap? and getting the first value in arraylist returns random bitmap? Thanks in advance.Interstellar
Because memory cache can contain different sizes (Bitmaps) of the same image. Actually you can deny it in configuration.Tannertannery
@NOSTRA , can u plz take a look at this post. #26480598Congressman
E
2

It should be MemoryCacheUtils, so you should use

MemoryCacheUtils.findCachedBitmapsForImageUri(imageUri, ImageLoader.getInstance().getMemoryCache());
Esra answered 7/6, 2014 at 17:49 Comment(0)
K
2

For disk cache use below code

public static boolean isDiskCache(String url) {
        File file = DiskCacheUtils.findInCache(url, ImageLoader.getInstance().getDiskCache());
        return file != null;
}
Kuebbing answered 5/3, 2017 at 8:1 Comment(0)
U
1

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

Ultra answered 2/10, 2014 at 12:56 Comment(0)
U
1

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)){
//
      }
Uproar answered 27/10, 2015 at 11:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.