Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Volley NetworkImageView clear cached image

Does anybody know if it is possible to clear the cached image from a single NetworkImageView using Googles Volley library?

I have an avatar image in a NetworkImageView and would like to show the new image once I have uploaded it to the server. At the moment if I do profileImg.setImageUrl("myUrl", mImageLoader); I get the cached image.

like image 792
Ivan Wooll Avatar asked Aug 13 '14 13:08

Ivan Wooll


3 Answers

Check this out :

1) Turning off cache : If you want disable the cache for a particular url, you can use setShouldCache() method as below.

StringRequest stringReq = new StringRequest(....);
stringReq.setShouldCache(false);

2) Deleting cache for particular URL : Use remove() to delete cache of an URL.

yourRequestQueue.getCache().remove(url);

3) Deleting all the cache :

yourRequestQueue.getCache().clear(url);

Also, check out this link here.

Hope this helps.

like image 101
Siddharth_Vyas Avatar answered Oct 08 '22 17:10

Siddharth_Vyas


So to belatedly answer my own question. I ended up taking a different approach to ensure that I always get the latest copy of an avatar image and it turned out to be very simple.

Instead of trying to clear out an individual image from the cache I use the following method.

int time = (int) (System.currentTimeMillis());//gets the current time in milliseconds
String myUrl = "www.mySite.com?timestamp=" + String.ValueOf(time);
profileImg.setImageUrl("myUrl", mImageLoader);

What this does is to append an imaginary parameter to the url I call to get the image with a current timestamp. This guarantees that I am always making a call to a unique url and so therefore am always getting the most up-to-date version of that image.

The beauty of this approach is that it is not limited to Volley and can be used however you choose to make network calls.

like image 43
Ivan Wooll Avatar answered Oct 08 '22 16:10

Ivan Wooll


in the LruBitmapCache you should do diable put(url, bitmap) :

 @Override
    public void putBitmap(String url, Bitmap bitmap) {
       // put(url, bitmap);
    }

by this way every time you call the volley method image don't save to catch

like image 26
h.hejabi Avatar answered Oct 08 '22 18:10

h.hejabi