1

I'm using Picasso for image loading, I use it with NetworkPolicy.OFFLINE, but at some point I want to update the image from internet and I'm trying with

Picasso.with(context).invalidate(url);
Picasso.with(context).load(url).memoryPolicy(MemoryPolicy.NO_CACHE, MemoryPolicy.NO_STORE).networkPolicy(NetworkPolicy.NO_CACHE);

But the image is still taken from disk, only the cache is invalidated.

Artjom B.
  • 61,146
  • 24
  • 125
  • 222
SpyZip
  • 5,511
  • 4
  • 32
  • 53

3 Answers3

1

Use this:

Picasso.with(context).load(url).memoryPolicy(MemoryPolicy.NO_CACHE).into(image);
Engr Waseem Arain
  • 1,163
  • 1
  • 17
  • 36
  • what's the difference, ".into()" ? I want to invalidate the cache on app start and then I still don't have views to load into. – SpyZip Mar 10 '16 at 11:44
0

You can clear cache programmatically using below code. You can use this code before you need to get images from API. If you want PICASSO to never cache images you can use code shared by waseem. but if you need it to cache but only sometimes you need to get images from API you can clear cache.

public static void trimCache(Context context) {
      try {
         File dir = context.getCacheDir();
         if (dir != null && dir.isDirectory()) {
            deleteDir(dir);
         }
      } catch (Exception e) {
         // TODO: handle exception
      }
   }


  public static boolean deleteDir(File dir) {
  if (dir != null && 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;
        }
     }
     return dir.delete();
  }
  else {
     return false;
  }
Developine
  • 12,483
  • 8
  • 38
  • 42
0

So I was calling this when I did not have any views available yet. The key is to call .into() method that triggers overwriting the cache. So I use only this:

Picasso.with(context).load(url).into(new ImageView(context));
SpyZip
  • 5,511
  • 4
  • 32
  • 53