Search code examples
androiduniversal-image-loader

UIL update cached images


I want to display Facebook profile picture by using URL but whenever user changes his profile picture, this library doesn't update cached image.

My UIL version is 1.9.3, display options:

    new DisplayImageOptions.Builder()
            .showImageOnLoading(android.R.color.white)
            .cacheInMemory(true)
            .cacheOnDisk(true)
            .considerExifParams(true)
            .build();

So how I should check if image in url changed? Or maybe you know another library which have this feature?

Thanks!


Solution

  • UIL has multithread image loading (async or sync). See library features on github.

    You enable caching on disc (device's file system or SD card) .cacheOnDisk(true). Library loads the image from this cache. Disable disc caching .cacheOnDisk(false) and write the following code in your Activity:

     @Override
    protected void onCreate(Bundle savedInstanceState) {    
        super.onCreate(savedInstanceState);
        ...
        DisplayImageOptions displayOptions = new DisplayImageOptions.Builder()
                    .showImageOnLoading(R.drawable.ic_preview)
                    .showImageForEmptyUri(R.drawable.ic_preview)
                    .showImageOnFail(R.drawable.ic_preview)
                    .cacheInMemory(true)
                    .cacheOnDisc(false)
                    .considerExifParams(true)
                    .build();
    
        ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(this)
                    .writeDebugLogs()
                    .defaultDisplayImageOptions(displayOptions)             
                    .build();
    
        ImageLoader imageLoader = ImageLoader.getInstance();
        imageLoader.init(config);
    
        AnimateFirstDisplayListener animate  = new AnimateFirstDisplayListener();
    
        imageLoader.displayImage(YourImageURL, userIv, animate);
        ...
    }
    
    public static class AnimateFirstDisplayListener extends SimpleImageLoadingListener {
    
        static final List<String> displayedImages = Collections.synchronizedList(new LinkedList<String>());
    
        @Override
        public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
            if (loadedImage != null) {
                ImageView imageView = (ImageView) view;
                boolean firstDisplay = !displayedImages.contains(imageUri);
                if (firstDisplay) {
                    FadeInBitmapDisplayer.animate(imageView, 500);
                    displayedImages.add(imageUri);
                }
            }
        }
    }