Clear Cache memory of Picasso

2019-01-14 08:19发布

I'm trying to clear the cache memory of Picasso via Android coding.

Can anyone please help me in this issue..?

I have tried using the following code, but this was not useful in my case:

Picasso.with(getActivity()).load(data.get(pos).getFeed_thumb_image()).skipMemoryCache().into(image);

8条回答
Root(大扎)
2楼-- · 2019-01-14 08:32

If you keep reference of your custom Downloader implementation you can clear cache.

public class PicassoUtil {
    private static Picasso sInstance;
    private static OkHttp22Downloader sDownloader;
    public  static Picasso getPicasso(Context context){
        if(sInstance == null) {
            sDownloader = new OkHttp22Downloader(context)
            Picasso.Builder builder = new Picasso.Builder(context);
            builder.downloader(sDownloader);
            sInstance = builder.build(sDownloader);
        }
        return sInstance;
    }
   public static void clearCache(){
      if(sDownloader != null){
        sDownloader.clearCache();
      }
   }
}

It is important to have access to your http client and its Cache. In my implementation there is access to the cache, hence clearing cache with clearCache() method.

查看更多
beautiful°
3楼-- · 2019-01-14 08:36

Remove cache of Picasso like this.

public class Clear {

    public static void clearCache (Picasso p) {
        p.cache.clear();
    }
}

This util class can clear the cache for you. You just have to call it:

Clear.clearCache(Picasso.with(context));

EDIT:
The class Clear must be in the package :

package com.squareup.picasso;

Because cache is not accessible from outside that package. Like in this answer: https://stackoverflow.com/a/23544650/4585226

查看更多
再贱就再见
4楼-- · 2019-01-14 08:36
 Picasso.with(this.getContext()).load(gamePlayer.getPlayerProfileUrl()).skipMemoryCache().into(iv);

This also works

查看更多
ら.Afraid
5楼-- · 2019-01-14 08:41

Use this instead :

 Picasso.with(getContext()).load(data.get(pos).getFeed_thumb_image()).memoryPolicy(MemoryPolicy.NO_CACHE).into(image);
查看更多
淡お忘
6楼-- · 2019-01-14 08:45

if you are trying to load an image through Json(from db) try clearing the networkCache for a better result.

Picasso.with(context).load(uri).networkPolicy(NetworkPolicy.NO_CACHE)
        .memoryPolicy(MemoryPolicy.NO_CACHE)
        .placeholder(R.drawable.bv_logo_default).stableKey(id)
        .into(viewImage_imageView);
查看更多
Luminary・发光体
7楼-- · 2019-01-14 08:45

i had the same problem. It worked for me. I used Picasso in RecycleView inside a dialog. When i closed dialog, picasso doesnt clear cache. But while you are using the dialog it clears image cache. However there is some cache that is not cleared. Maybe the cache that was not cleared is the last you seen in dialog before dialog.dismiss().

use this memoryPolicy(MemoryPolicy.NO_CACHE,MemoryPolicy.NO_STORE)

Picasso.with(activity).load(file).resize(100,100).centerCrop().memoryPolicy(MemoryPolicy.NO_CACHE,MemoryPolicy.NO_STORE).into(contactImage, new com.squareup.picasso.Callback() {
               @Override
                public void onSuccess() {

                }

                @Override
                public void onError() {

                }
            });
查看更多
登录 后发表回答