使Picasso缓存失效

77
我使用Picasso从磁盘加载图像,例如:Picasso.with(ctx).load(new File("/path/to/image")).into(imageView),但是当我在该文件中保存新图像并刷新我的ImageView时,Picasso仍然缓存了位图。
是否可以使Picasso的缓存失效?

1
在执行完Picasso.with(getActivity()).invalidate(file);之后,我该如何重新缓存呢? - tux-world
14个回答

1
您可以通过skipMemoryCache()跳过内存缓存。
请参见以下内容。
        Picasso.with(this)
            .load(IMAGE_URL)
            .skipMemoryCache()
            .placeholder(R.drawable.placeholder)
            .error(R.drawable.no_image)
            .into(mImageViewPicasso);

Gradle compile "com.squareup.picasso:picasso:2.4.0"

意思是在Gradle中添加依赖,引入Picasso 2.4.0版本库。

0

请使用shutdown()代替。 根据源代码,shutdown将停止接受进一步的请求并清除所有缓存。

 /** Stops this instance from accepting further requests. */
  public void shutdown() {
    if (this == singleton) {
      throw new UnsupportedOperationException("Default singleton instance cannot be shutdown.");
    }
    if (shutdown) {
      return;
    }
    cache.clear();
    cleanupThread.shutdown();
    stats.shutdown();
    dispatcher.shutdown();
    for (DeferredRequestCreator deferredRequestCreator : targetToDeferredRequestCreator.values()) {
      deferredRequestCreator.cancel();
    }
    targetToDeferredRequestCreator.clear();
    shutdown = true;
  }

同时,您不能关闭单例实例。因此,您需要为Picasso设置实例变量。不要忘记在每次shutdown()时重新初始化picasso实例以便重复使用它。


0
File f = new File(path, name);
Picasso.with(this).invalidate(Uri.fromFile(f));

0

另一个选项是将新图像保存到与原始文件不同的文件中。由于Picasso位图缓存是基于文件路径的键,从不同的文件加载新图像将导致缓存未命中。这也有一个附带好处,就是不必清除整个缓存。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接