【问题标题】:Clear Cache memory of Picasso清除毕加索的缓存
【发布时间】:2015-02-14 15:32:49
【问题描述】:

我正在尝试通过 Android 编码清除毕加索的缓存。

谁能帮我解决这个问题..?

我曾尝试使用以下代码,但这在我的情况下没有用:

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

【问题讨论】:

  • 尚未测试,但我猜Picasso.with(context).shutdown() 可能会有所帮助。

标签: android caching memory picasso


【解决方案1】:

改用这个:

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

【讨论】:

  • 对我不起作用。因此我也添加了 .networkpolicy(NetworkPolicy.NO_CACHE) 。然后才起作用。
  • 如果想缓存所有图像一段时间,但当我不再需要它时清除所有缓存呢?
【解决方案2】:

像这样删除毕加索的缓存。

public class Clear {

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

这个 util 类可以为你清除缓存。你只需要调用它:

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

编辑
类 Clear 必须在包中:

package com.squareup.picasso;

因为无法从该包外部访问缓存。 就像在这个答案中一样:https://stackoverflow.com/a/23544650/4585226

【讨论】:

  • 感谢您的回答。将方法更改为非静态或将类更改为静态会引发错误。它还说“缓存”在以下行中不可见: p.cache.clear();
  • 它应该可以工作。我们正在调用公共类的静态方法。
  • 我只使用了 Picasso jar,没有使用任何外部 picasso 库。它说“p.cache”是不可见的。
  • 你应该为你的Clear类设置同一个Picasso类的包。
  • 关于如何访问 jar 中不公开的方法的概念加 1
【解决方案3】:

如果您尝试通过 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);

【讨论】:

  • 对于我们这些抽象毕加索的人,我们默认做什么?
  • 图片需要很长时间才能加载
【解决方案4】:

如果想用给定的 Uri 刷新图像,而不是清除整个缓存。试试这个Picasso.with(context).invalidate(uri); 它会在内部从 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()); }

【讨论】:

  • 这对我来说非常有效,并且确实比清除整个缓存更有效。这应该是公认的答案。
【解决方案5】:

当活动销毁时,如果我们使用毕加索,不幸的是位图没有被回收。我尝试以编程方式回收位图,即加载到图像视图中的内容。有一种方法可以使用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();
            }
        }
    };

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

但请记住,这种情况下不要在内存中缓存图像,这会导致使用回收位图异常。

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

希望对您有所帮助。

【讨论】:

    【解决方案6】:

    如果您保留对 custom 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();
          }
       }
    }
    

    访问您的 http 客户端及其Cache 非常重要。在我的实现中可以访问缓存,因此使用clearCache() 方法清除缓存。

    【讨论】:

      【解决方案7】:

      我有同样的问题。 它对我有用。 我在对话框内的 RecycleView 中使用了 Picasso。当我关闭对话框时,毕加索没有清除缓存。但是当您使用该对话框时,它会清除图像缓存。但是有一些缓存没有被清除。也许未清除的缓存是您在 dialog.dismiss() 之前在对话框中看到的最后一个。

      使用这个 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() {
      
                      }
                  });
      

      【讨论】:

        【解决方案8】:
         Picasso.with(this.getContext()).load(gamePlayer.getPlayerProfileUrl()).skipMemoryCache().into(iv);
        

        这也有效

        【讨论】:

          猜你喜欢
          • 2016-08-10
          • 2014-04-10
          • 2015-05-07
          • 2016-03-17
          • 2016-12-31
          • 2019-05-26
          • 2016-01-20
          • 2014-11-03
          • 1970-01-01
          相关资源
          最近更新 更多