I'm loading dynamically generated images so I always want them to be up to date. But they take time to load so I also want to display a cached version while the updated one doesn't come. How can I do this with Universal Image Loader?
More specifically, when I call "displayImage" I want it to do the following:
- If a cached image exists display it right away.
- Start downloading from the given url anyways.
- When the image loading finishes, display it in the view replacing the cached image.
- Update the cache.
So in the end I used an ImageLoadingListener as follows:
onLoadingStarted: Check for cache when loading starts.
onLoadingComplete: If no cache was found then do nothing. The request will be sent to network and cache will be updated naturally. Otherwise clear cache and call displayImage again (no listener needed this time). The cached image will be shown in the view normally. Moreover, when the 2nd loading finishes, view and cache will be updated.
You can use the ImageLoadingListener. This interface has 4 methods to override: onLoadingStarted,onLoadingFailed,onLoadingComplete,onLoadingCancelled. In onLoadingStarted you can make the image the cached one, then on completed you change it.
So the call would look like this: