Search code examples
androidcachinguniversal-image-loader

Android-ImageLoader => When I call image with the same name from server, imageLoader takes older one from Cache. How can I fix it?


Firstly my static config file is like that:

public static DisplayImageOptions getDisplayImageOptions(){
    options = new DisplayImageOptions.Builder()
            .showImageForEmptyUri(R.mipmap.ic_empty)
            .showImageOnFail(R.mipmap.ic_error)
            .resetViewBeforeLoading(true)
            .cacheInMemory(true)
            .cacheOnDisk(true)
            .imageScaleType(ImageScaleType.EXACTLY)
            .bitmapConfig(Bitmap.Config.RGB_565)
            .considerExifParams(true)
            .displayer(new FadeInBitmapDisplayer(300))
            .build();
    return options;
}

public static ImageLoaderConfiguration getImageLoaderConfiguration(Context context){
    config = new ImageLoaderConfiguration.Builder(context)
            .memoryCacheExtraOptions(480, 800) // default = device screen dimensions
            .diskCacheExtraOptions(480, 800, null)
            .threadPriority(Thread.NORM_PRIORITY - 2) // default
            .tasksProcessingOrder(QueueProcessingType.FIFO) // default
            .denyCacheImageMultipleSizesInMemory()
            .memoryCache(new LruMemoryCache(2 * 1024 * 1024))
            .memoryCacheSize(2 * 1024 * 1024)
            .memoryCacheSizePercentage(13) // default
            .diskCacheSize(50 * 1024 * 1024)
            .diskCacheFileCount(100)
            .defaultDisplayImageOptions(DisplayImageOptions.createSimple()) // default
            .writeDebugLogs()
            .build();

    return config;
}

My server includes image called: example.jpg When I call from my android application there isn't any problem. But if I change this image with another image and its name is the same(example.jpg), when I call this image again ,my android application gets old(cached) image.

I think imageLoader must understand cached image and server image are different even if they have the same name. But I cannot figure it out.

How can I fix it ?

    imageLoader = ImageLoader.getInstance();
    ImageLoaderConfiguration config = ConfigFile.getImageLoaderConfiguration(getActivity());
    ImageLoader.getInstance().init(config);

    imageLoader.displayImage(newImageUrl, editProfileImageView, ConfigFile.getDisplayImageOptions(), new ImageLoadingListener() {
        @Override
        public void onLoadingStarted(String imageUri, View view) {

        }

        @Override
        public void onLoadingFailed(String imageUri, View view, FailReason failReason) {

        }

        @Override
        public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {

        }

        @Override
        public void onLoadingCancelled(String imageUri, View view) {

        }
    }, new ImageLoadingProgressListener() {
        @Override
        public void onProgressUpdate(String imageUri, View view, int current, int total) {

        }
    });

Solution

  • Set cacheOnDisk(false) and cacheInMemory(false) in DisplayImageOptions configuration for loading such dynamic nature of images.

    Eg:

    imageLoader = ImageLoader.getInstance();
    ImageLoaderConfiguration config = ConfigFile.getImageLoaderConfiguration(getActivity());
    ImageLoader.getInstance().init(config);
    
    DisplayImageOptions options = ConfigFile.getDisplayImageOptions();
    options.cacheOnDisk(false);
    options.cacheInMemory(false);
    
    
    
    imageLoader.displayImage(newImageUrl, editProfileImageView, options, new ImageLoadingListener() {
        @Override
        public void onLoadingStarted(String imageUri, View view) {
    
        }
    
        @Override
        public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
    
        }
    
        @Override
        public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
    
        }
    
        @Override
        public void onLoadingCancelled(String imageUri, View view) {
    
        }
    }, new ImageLoadingProgressListener() {
        @Override
        public void onProgressUpdate(String imageUri, View view, int current, int total) {
    
        }
    });