How can I remove an image from the cache using Volley?

I am working on an application where a user can change their profile picture, which is actually stored on the server like this: http://serveraddress.com/user-pictures/user_id , the problem is because volleyball uses Image Cache even after the user has changed the profile picture (uploaded to the server), the cached picture will be displayed instead of the "updated" picture.

I tried to remove the image from the cache and also invalidate the image url (because it's the same) but it doesn't work.

getRequestQueue().getCache().invalidate("http://serveraddress.com/user-pictures/122432", true); //invalidate
mLRUCache.remove("http://serveraddress.com/user-pictures/122432");

      

Any suggestions would be appreciated. Thanks in advance.

+3


source to share


1 answer


I faced the same problem. After a lot of research, I've found that the way images are cached on disk is according to the HTTP cache headers. If the cache headers say the image is still valid, then it will not be requested again from the server. And since the new image you are saving has the same name, the volleyball will not request an image from the server. So, I applied this simple logic:

Let's say initially your path to the picture is xxx.pic.jpg.



Get a random number, say x, from any random number generator function. Just add x to your profile path i.e. Your new image path will look like xxx.pic.x.jpg. It should solve the problem :)

0


source







All Articles