Home » Android » Clear Disk/SD Card Cache of Android's Picasso Image Library

Clear Disk/SD Card Cache of Android's Picasso Image Library

Posted by: admin June 16, 2020 Leave a comment

Questions:

I am using Picasso to load images from my server. It works fine, but I am loading an image, and changed it later. But Picasso has the image cached somewhere in disk (I checked the SD card, and could not find any directory that Picasso is storing in).

I tried to remove the cache as suggested by the accepted answer to this question : Invalidate cache in Picasso

I also tried skipping cache when loading images using : Picasso.with(ctx).load(new File(“/path/to/image”)).skipMemoryCache().into(imageView)

But none of these methods are working.

Thanks for any suggestion or hint that could help me pass this problem.

How to&Answers:

Picasso disk images are cached in app internal cache directory . Look at the method createDefaultCacheDir here https://github.com/square/picasso/blob/master/picasso/src/main/java/com/squareup/picasso/Utils.java

You can clear all the images in getCacheDir/picasso-cache like this

   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 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();
}

Answer:

You can read this entry https://stackoverflow.com/a/18964588/2358095 to understand how picasso disk cache works.

So first of all you have to add static void delete(Object cache) method in ResponseCacheIcs class. This class is defined in UrlConnectionDownloader.java. It seems like that:

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 have to add

void clearDiskCache();

method in Downloader.java. Then you have to add unimplemented method in UrlConnectionDownloader.java and OkHttpDownloader.java. You should define public void clearDiskCache() method in UrlConnectionDownloader.java like this:

@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 have to add:

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

method in Dispacher.java. And then add:

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

method in Picasso.java.

Bingo!!! Now you can call 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);