简体   繁体   English

清除Picasso的缓存

[英]Clear Cache memory of Picasso

I'm trying to clear the cache memory of Picasso via Android coding. 我试图通过Android编码清除Picasso的缓存。

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

请改用:

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

Remove cache of Picasso like this. 像这样删除Picasso的缓存。

public class Clear {

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

This util class can clear the cache for you. 此util类可以为您清除缓存。 You just have to call it: 你只需要调用它:

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

EDIT : 编辑
The class Clear must be in the package : Clear类必须在包中:

package com.squareup.picasso;

Because cache is not accessible from outside that package. 因为无法从该包外部访问缓存。 Like in this answer: https://stackoverflow.com/a/23544650/4585226 就像在这个答案: 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. 如果您尝试通过Json(从db)加载图像,请尝试清除networkCache以获得更好的结果。

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. 当活动破坏时,遗憾的是如果我们使用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引用加载的位图。

 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. 当Activity破坏时,我调用onBitmapFailed(null)来回收加载的位图。

@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. 如果想要使用给定的Uri刷新图像,而不是清除完整的缓存。 try this Picasso.with(context).invalidate(uri); 试试这个Picasso.with(context).invalidate(uri); it internally removes the key from the cache maintained by Picasso. 它在内部从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()); } 摘自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()); } /** * 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. 如果继续引用自定义 Downloader实现,则可以清除缓存。

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 . 访问您的http客户端及其Cache非常重要。 In my implementation there is access to the cache, hence clearing cache with clearCache() method. 在我的实现中,可以访问缓存,因此使用clearCache()方法清除缓存。

i had the same problem. 我有同样的问题。 It worked for me. 它对我有用。 I used Picasso in RecycleView inside a dialog . 在对话框中的RecycleView中使用了Picasso 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(). 也许未清除的缓存是您在dialog.dismiss()之前在对话框中看到的最后一个缓存。

use this memoryPolicy(MemoryPolicy.NO_CACHE,MemoryPolicy.NO_STORE) 使用此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);

这也有效

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM