I load an image from disk using Picasso, e.g., Picasso.with(ctx).load(new File("/path/to/image")).into(imageView)
, but whenever I save a new image in that file, and refresh my ImageView
, Picasso still has the bitmap cached.
Is it possible to invalidate the cache in Picasso?
Another option is to save the new image into a different file than the original. Since the Picasso bitmap cache is keyed off of the file path, loading the new image from a different file will result in a cache miss. This also has the side benefit of not having to clear the entire cache.
You can skip memory cache by
skipMemoryCache()
see the following
gradle
compile "com.squareup.picasso:picasso:2.4.0"
Abort memory cache and disk cache check by indicate memory policy by flag: emoryPolicy.NO_CACHE and NetworkPolicy.NO_CACHE as below code snippet:
The order of search image in Picasso is: Memory cache -> Disk cache -> Network
So there are few scenario we need to invalidate cache in Picasso:
1.Invalidate memory cache:
Solution: Clear cache of Url, File, Uri if exist
.
2.Invalidate memory cache and disk cache Online
※note: Online mean update directly to ImageView
User case: Image updated on remote host
Solution: Abort image on memory cache and disk cache then request image on remote host
-> Abort memory cache and disk cache
.
3.Invalidate memory cache and disk cache Offline
※ note: Offline mean update not update to ImageView, just background fetch to using later
Solution: fetch only
※Note: Using fetch() is good but it also consume network resource, so please consider carefully, check scenario 4 in below for better solution
4.Invalidate memory cache and disk cache Offline if disk cache is exist
Solution: Should check disk by using parameter: NetworkPolicy.OFFLINE cache before fetch
Picasso is an amazing libs, I hope squareup will add more convenience API to manage cache in upcoming future.
use
shutdown()
instead As per source code; shutdown will stop accepting further request as well as clear all cacheAlso you can not shutdown singleton instance. So you need to have instance variable for
Picasso
. Do not forget to reinitialize picasso instance everytime youshutdown()
it in order to reuse itWhat you can do if you want to delete all cache at once, is to create a custom
Picasso.LruCache
, and then use theclear
method on it.Here is a sample:
To clear the cache: