android universal image loader clear cache
You can use DiskCacheUtils
and MemoryCacheUtils
to remove specific image by image url
DiskCacheUtils.removeFromCache(imageUrl, ImageLoader.getInstance().getDiskCache());
MemoryCacheUtils.removeFromCache(imageUrl, ImageLoader.getInstance().getMemoryCache());
or to completely clean cache
ImageLoader.getInstance().clearMemoryCache()
ImageLoader.getInstance().clearDiskCache()
To clear cache when activity created you can call this methods after you initialized your ImageLoader
To clear cache on leaving the activity you can call methods in onDestroy ()
, but i should mind that system can kill your activity without calling this method.
Universal Image Loader: Can I use cache but also refresh it?
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.
ImageLoader.getInstance().displayImage(imageUri, view, new SimpleImageLoadingListener() {
boolean cacheFound;
@Override
public void onLoadingStarted(String url, View view) {
List<String> memCache = MemoryCacheUtils.findCacheKeysForImageUri(url, ImageLoader.getInstance().getMemoryCache());
cacheFound = !memCache.isEmpty();
if (!cacheFound) {
File discCache = DiscCacheUtils.findInCache(url, ImageLoader.getInstance().getDiscCache());
if (discCache != null) {
cacheFound = discCache.exists();
}
}
}
@Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
if (cacheFound) {
MemoryCacheUtils.removeFromCache(imageUri, ImageLoader.getInstance().getMemoryCache());
DiscCacheUtils.removeFromCache(imageUri, ImageLoader.getInstance().getDiscCache());
ImageLoader.getInstance().displayImage(imageUri, (ImageView) view);
}
}
});
}
How To Use universal image loader offline caching?
You can use the ImageLoaderConfiguration.Builder
class to customize disk caching. This includes the methods:
diskCacheExtraOptions()
diskCacheSize()
(in bytes).diskCacheFileCount()
diskCacheFileNameGenerator()
- and some others.
Or you can just use diskCache(DiskCache)
to provide a custom class for implementing offline caching.
From the example in the Configuration section of the wiki:
File cacheDir = StorageUtils.getCacheDirectory(context);
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context)
.memoryCacheExtraOptions(480, 800) // default = device screen dimensions
.diskCacheExtraOptions(480, 800, null)
.taskExecutor(...)
.taskExecutorForCachedImages(...)
.threadPoolSize(3) // default
.threadPriority(Thread.NORM_PRIORITY - 1) // default
.tasksProcessingOrder(QueueProcessingType.FIFO) // default
.denyCacheImageMultipleSizesInMemory()
.memoryCache(new LruMemoryCache(2 * 1024 * 1024))
.memoryCacheSize(2 * 1024 * 1024)
.memoryCacheSizePercentage(13) // default
.diskCache(new UnlimitedDiscCache(cacheDir)) // default
.diskCacheSize(50 * 1024 * 1024)
.diskCacheFileCount(100)
.diskCacheFileNameGenerator(new HashCodeFileNameGenerator()) // default
.imageDownloader(new BaseImageDownloader(context)) // default
.imageDecoder(new BaseImageDecoder()) // default
.defaultDisplayImageOptions(DisplayImageOptions.createSimple()) // default
.writeDebugLogs()
.build();
Related Topics
Swiperefreshlayout + Webview When Scroll Position Is at Top
Folder Added on Android Is Not Visible via Usb
Adding Multiple Markers in Google Maps API V2 Android
Error: Mapfragment Cannot Be Cast to Android.Support.V4.App.Fragment
No Peer Certificate' Error in Android 2.3 But Not in 4
Creating a Dialogpreference from Xml
Moving from One Activity to Another Activity in Android
Include Stetho Only in the Debug Build Variant
Strange Behavior of Android Videoview - "Can't Play Video"
How to Limit Fling in Android Gallery to Just One Item Per Fling
Communicating with Serial Usb Device Over Android
Unexpected Namespace Prefix "Xmlns" Found for Tag Linearlayout
Android Class Parcelable with Arraylist
Android Test Code Coverage with Jacoco Gradle Plugin
Android Listview: Get Data Index of Visible Item
How Can Retrofit 2.0 Parse Nested JSON Object
Opening Infowindow Automatically When Adding Marker Google Maps V2 Android