Search code examples
androiduniversal-image-loader

Universal Image loader not refresh image if changed from server with same URL


  • I have used Universal Image Loader for load image from a server and also getting cached it into memory for fast loading.
  • But from server side used the same URL for update image.
  • For eg., www.example.com/xyz.png is URL for the image when they needed to update image they return same URL with a different image.
  • In this case, Universal Image Loader return image which is previously cached in memory (I think it cached image using its related URL).
  • So, I needed to Change image if URL returned a different image.

This is my code which I use for load image

DisplayImageOption.java

public class DisplayImageOption {

public static DisplayImageOptions getDisplayImage() {
    // .displayer(new RoundedBitmapDisplayer(0))
    return new DisplayImageOptions.Builder()
            .showImageOnLoading(R.mipmap.icon_place_holder)
            .showImageForEmptyUri(R.mipmap.icon_place_holder)
            .showImageOnFail(R.mipmap.icon_place_holder)
            .cacheInMemory(true)
            .cacheOnDisk(true)
            .considerExifParams(true).build();
}

public static DisplayImageOptions getDisplayRoundedImage() {
    return new DisplayImageOptions.Builder()
            .showImageOnLoading(R.mipmap.icon_place_holder)
            .showImageForEmptyUri(R.mipmap.icon_place_holder)
            .showImageOnFail(R.mipmap.icon_place_holder)
            .cacheInMemory(true)
            .cacheOnDisk(true)
            .considerExifParams(true)
            .displayer(new RoundedBitmapDisplayer(100)).build();
    }
}

Code for Image Loading

ImageLoader.getInstance().displayImage(url, imageView, DisplayImageOption.getDisplayImage());

Thanks


Solution

  • In your ImageLoaderConfiguration add diskCache option.

    File cacheDir = StorageUtils.getCacheDirectory(context);
    long cacheAge = 10L;
    
    ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context)
            .diskCache(new LimitedAgeDiscCache(cacheDir, cacheAge)) // this will make the cache to remain for 10 seconds only
            .build();
    

    Then set it on ImageLoader and display image with your DisplayImageOption

    ImageLoader.getInstance().init(config);
    ImageLoader.getInstance().displayImage(url, imageView, DisplayImageOption.getDisplayImage());
    

    What it does? Taken from Android-Universal-Image-Loader

    LimitedAgeDiscCache (Size-unlimited cache with limited files' lifetime. If age of cached file exceeds defined limit then it will be deleted from cache.)

    And this piece of code is from Android-Universal-Image-Loader's LimitedAgeDiskCache.java class.

    /**
         * @param cacheDir Directory for file caching
         * @param maxAge   Max file age (in seconds). If file age will exceed this value then it'll be removed on next
         *                 treatment (and therefore be reloaded).
         */
        public LimitedAgeDiskCache(File cacheDir, long maxAge) {
            this(cacheDir, null, DefaultConfigurationFactory.createFileNameGenerator(), maxAge);
    }
    

    You may like this approach as well.