Clear Cache memory of Picasso

六月ゝ 毕业季﹏ 提交于 2019-11-27 20:45:34

Use this instead :

 Picasso.with(getContext()).load(data.get(pos).getFeed_thumb_image()).memoryPolicy(MemoryPolicy.NO_CACHE).into(image);
Murtaza Khursheed Hussain

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

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

When activity destroy, unfortunately bitmap was not recycled if we're using Picasso. I try to programmatically recycle bitmap, what's loaded in to image view. There is a way to reference to loaded bitmap by using Target.

 Target mBackgroundTarget = new Target() {
        Bitmap mBitmap;

        @Override
        public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
            if (bitmap == null || bitmap.isRecycled())
                return;

            mBitmap = bitmap;
            mBgImage.setImageBitmap(bitmap);
            mHandler.post(new Runnable() {
                @Override
                public void run() {
                    // Do some animation
                }
            });
        }

        @Override
        public void onBitmapFailed(Drawable errorDrawable) {
            recycle();
        }

        @Override
        public void onPrepareLoad(Drawable placeHolderDrawable) {

        }

        /**
         * Recycle bitmap to free memory
         */
        private void recycle() {
            if (mBitmap != null && !mBitmap.isRecycled()) {
                mBitmap.recycle();
                mBitmap = null;
                System.gc();
            }
        }
    };

And when Activity destroy, I call onBitmapFailed(null) to recycle loaded bitmap.

@Override
protected void onDestroy() {
    super.onDestroy();
    try {
        if (mBackgroundTarget != null) {
            mBackgroundTarget.onBitmapFailed(null);
            Picasso.with(context).cancelRequest(mBackgroundTarget);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

But remember, DON'T CACHE IMAGE IN MEMORY by this case, It will cause Use recycled bitmap exception.

Picasso.with(context)
            .load(imageUrl)
            .resize(width, height)
            .memoryPolicy(MemoryPolicy.NO_CACHE)
            .into(mBackgroundTarget);

Hope this help.

Instead of clearing the complete cache if one wants to refresh the image with the given Uri. try this Picasso.with(context).invalidate(uri); it internally removes the key from the cache maintained by Picasso.

Excerpt from Picasso.java /** * Invalidate all memory cached images for the specified {@code uri}. * * @see #invalidate(String) * @see #invalidate(File) */ public void invalidate(Uri uri) { if (uri == null) { throw new IllegalArgumentException("uri == null"); } cache.clearKeyUri(uri.toString()); }

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.

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() {

                }
            });
 Picasso.with(this.getContext()).load(gamePlayer.getPlayerProfileUrl()).skipMemoryCache().into(iv);

This also works

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!