Clear Drive / SD Cache for Android Picasso Image Library Cache

I use Picasso to download images from my server. It works fine, but I upload the image and change it later. But Picasso has an image cached somewhere on the disk (I checked the SD card and could not find the directory where Picasso is located).

I tried to delete the cache, as suggested by the accepted answer to this question: Invalid cache in Picasso

I also tried to skip the cache when loading images using: Picasso.with (ctx) .load (new file ("/ path / to / image")). skipMemoryCache (). in (imageView)

But none of these methods work.

Thanks for any suggestion or hint that can help me solve this problem.

+8
android caching image picasso
source share
2 answers

Picasso Disk images are cached in the application’s internal cache directory. Take a look at the createDefaultCacheDir method here https://github.com/square/picasso/blob/master/picasso/src/main/java/com/squareup/picasso/Utils.java

You can clear all images in getCacheDir/picasso-cache as follows

  public boolean clearImageDiskCache() { File cache = new File(mContext.getApplicationContext().getCacheDir(), "picasso-cache"); if (cache.exists() && cache.isDirectory()) { return deleteDir(cache); } return false; } 

To delete all files in a directory

 public static boolean deleteDir(File dir) { if (dir.isDirectory()) { String[] children = dir.list(); for (int i = 0; i < children.length; i++) { boolean success = deleteDir(new File(dir, children[i])); if (!success) { return false; } } } // The directory is now empty so delete it return dir.delete(); } 
+4
source share

You can read this post at https://stackoverflow.com/a/166268/127 to understand how picasso disk cache works.

First of all, you need to add the static void delete(Object cache) method to the ResponseCacheIcs class. This class is defined in UrlConnectionDownloader.java. It looks like this:

 private static class ResponseCacheIcs { static Object install(Context context) throws IOException { File cacheDir = Utils.createDefaultCacheDir(context); HttpResponseCache cache = HttpResponseCache.getInstalled(); if (cache == null) { long maxSize = Utils.calculateDiskCacheSize(cacheDir); cache = HttpResponseCache.install(cacheDir, maxSize); } return cache; } static void close(Object cache) { try { ((HttpResponseCache) cache).close(); } catch (IOException ignored) { } } static void delete(Object cache) { try { ((HttpResponseCache) cache).delete(); } catch (IOException ignored) { } } } 

After that you should add

 void clearDiskCache(); 

in Downloader.java. Then you must add the unrealized method to UrlConnectionDownloader.java and OkHttpDownloader.java. You must define the public void clearDiskCache() method in UrlConnectionDownloader.java as follows:

 @Override public void clearDiskCache() { // TODO Auto-generated method stub if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH && cache != null) { ResponseCacheIcs.delete(cache); } } 

Then you should add:

 void clearDiskCache(){ downloader.clearDiskCache(); } 

in Dispacher.java. Then add:

 public void clearDiskCache(){ dispatcher.clearDiskCache(); } 

in Picasso.java.

Bingo!!! Now you can call the clearDiskCache() method in your code. Here is an example:

 Picasso picasso = Picasso.with(TestActivity.this); picasso.clearDiskCache(); picasso.setDebugging(true); picasso.setIndicatorsEnabled(true); picasso.setLoggingEnabled(true); picasso.load(imageURL).into(imageView); 
+1
source share

All Articles