I am using Picasso to show one's portrait, when the protrait is changed, I want to clear this user's cache(or all users' portrait cache), here is my code, and it doesn't work,anyone can help me?
LruCache lruCache = new LruCache(context);
lruCache.clear();
Picasso picasso = new Picasso.Builder(context).memoryCache(lruCache).build();
picasso.load(portraitUrl).resize(50, 50).centerCrop().error(R.drawable.user_portrait).into(portaitView);
In the recent versions of Picasso, there is a new method for invalidate, without any workarounds, so I think that custom PicassoTools class mentioned earlier, is now obsolete
I was facing the same problem and none of the solutions was acceptable for me... So I at least made a solution with reflection. I don't use crop, resize and so on, so my clear function only works if you only use
load(url)
with resizing, cropping, transformations... But I customized the completegetKey
function, so this should at least be a big help to extend the clear function. Or just make thegetKey
function public and change theclear(uri)
function toclear(key)
...I just copied the key function from the source code and adopted it.
Just get the picasso instance from my tools class and everything should work (instead of having an context provider like me, you can for example add a init function, to initialise the PicassoTools class, with for example the application context).
Just use the following tools class:
And the extended cache class:
PS: inspired by Invalidate cache in Picasso