I am trying to disable cache in in the NetworkImageView which Volley class in my app. I tried this code but it does not remove the cache.
mNetworkImageView =
You can try the following (inside VolleySingleton
class):
mImageLoader = new ImageLoader(mRequestQueue, new ImageLoader.ImageCache() {
@Override
public Bitmap getBitmap(String url) {
return null;
}
@Override
public void putBitmap(String url, Bitmap bitmap) {
}
});
You can check when debugging, set breakpoint at Bitmap cachedBitmap = mCache.getBitmap(cacheKey);
line inside ImageLoader.java
, you will find cachedBitmap
null.
Or put Log.w("cachedBitmap", "Bitmap cached!");
as my following code to check:
public ImageContainer get(String requestUrl, ImageListener imageListener,
int maxWidth, int maxHeight, ScaleType scaleType) {
// only fulfill requests that were initiated from the main thread.
throwIfNotOnMainThread();
final String cacheKey = getCacheKey(requestUrl, maxWidth, maxHeight, scaleType);
// Try to look up the request in the cache of remote images.
Bitmap cachedBitmap = mCache.getBitmap(cacheKey);
if (cachedBitmap != null) {
Log.w("cachedBitmap", "Bitmap cached!");
// Return the cached bitmap.
ImageContainer container = new ImageContainer(cachedBitmap, requestUrl, null, null);
imageListener.onResponse(container, true);
return container;
}
// The bitmap did not exist in the cache, fetch it!
ImageContainer imageContainer =
new ImageContainer(null, requestUrl, cacheKey, imageListener);
// Update the caller to let them know that they should use the default bitmap.
imageListener.onResponse(imageContainer, true);
// Check to see if a request is already in-flight.
BatchedImageRequest request = mInFlightRequests.get(cacheKey);
if (request != null) {
// If it is, add this request to the list of listeners.
request.addContainer(imageContainer);
return imageContainer;
}
// The request is not already in flight. Send the new request to the network and
// track it.
Request<Bitmap> newRequest = makeImageRequest(requestUrl, maxWidth, maxHeight, scaleType,
cacheKey);
mRequestQueue.add(newRequest);
mInFlightRequests.put(cacheKey,
new BatchedImageRequest(newRequest, imageContainer));
return imageContainer;
}
Hope it helps!
Google Volley provides 2 ways to clear an item from the Cache:
AppController.getInstance().getRequestQueue().getCache().remove(key);
And
AppController.getInstance().getRequestQueue().getCache().invalidate(key, fullExpire);
remove() means you are removing the actual cached data.
invalidate() means you are just marking the data as invalid. So volley will check with the server whether the data is still valid. The full expire determines whether to use the data before volley has validated it with the server.
To clear cache in each 30 minutes use below code:-
you can use volley's serverDate to get the date for when the response was originally received as
AppController.getInstance().getRequestQueue().getCache().get(url).serverDate
So in your code use getMinutesDifference function as
public static long getMinutesDifference(long timeStart,long timeStop){
long diff = timeStop - timeStart;
long diffMinutes = diff / (60 * 1000);
return diffMinutes;
}
And call the function as
Calendar calendar = Calendar.getInstance();
long serverDate = AppController.getInstance().getRequestQueue().getCache().get(url).serverDate;
if(getMinutesDifference(serverDate, calendar.getTimeInMillis()) >=30){
AppController.getInstance().getRequestQueue().getCache().invalidate(url, true);
}
It will invalidate the cache,if previous url response >=30 minutes.
Hope it helps !