清除安卓Picasso图片库的磁盘/SD卡缓存

8
我正在使用Picasso从我的服务器加载图像。它能正常工作,但我之后改变了图片。但是,Picasso将图片缓存在磁盘的某个地方(我检查了SD卡,但找不到任何存储目录)。
我尝试按照这个问题的被接受答案所建议的方式删除缓存:Invalidate cache in Picasso 我还尝试跳过使用“Picasso.with(ctx).load(new File("/path/to/image")).skipMemoryCache().into(imageView)”来加载图像时的缓存,但这些方法都没有起作用。
感谢任何可以帮助我解决这个问题的建议或提示。

缓存仅用于内存,而不是磁盘。我对磁盘也有同样的问题。 - Johnny Z
检查我的答案,可能会有帮助。https://dev59.com/dF4c5IYBdhLWcg3wz9BG#27866219 - Nikola Despotoski
2个回答

8

Picasso磁盘映像被缓存在应用程序内部缓存目录中。在此处查看createDefaultCacheDir方法:https://github.com/square/picasso/blob/master/picasso/src/main/java/com/squareup/picasso/Utils.java

您可以像这样清除getCacheDir/picasso-cache中的所有图像。

   public boolean clearImageDiskCache() {
       File cache = new File(mContext.getApplicationContext().getCacheDir(), "picasso-cache");
            if (cache.exists() && cache.isDirectory()) {
                return deleteDir(cache);
            }
    return false;
}

删除目录中的所有文件
public static boolean deleteDir(File dir) {
    if (dir.isDirectory()) {
        String[] children = dir.list();
        for (int i = 0; i < children.length; i++) {
            boolean success = deleteDir(new File(dir, children[i]));
            if (!success) {
                return false;
            }
        }
    }
    // The directory is now empty so delete it
    return dir.delete();
}

1
你可以阅读这篇文章https://dev59.com/HmMk5IYBdhLWcg3w0RGU#18964588了解Picasso磁盘缓存的工作原理。
首先,你需要在ResponseCacheIcs类中添加static void delete(Object cache)方法。该类定义在UrlConnectionDownloader.java中。它看起来像这样:
private static class ResponseCacheIcs {
    static Object install(Context context) throws IOException {
      File cacheDir = Utils.createDefaultCacheDir(context);
      HttpResponseCache cache = HttpResponseCache.getInstalled();
      if (cache == null) {
        long maxSize = Utils.calculateDiskCacheSize(cacheDir);
        cache = HttpResponseCache.install(cacheDir, maxSize);
      }
      return cache;
    }

    static void close(Object cache) {
      try {
        ((HttpResponseCache) cache).close();
      } catch (IOException ignored) {
      }
    }

    static void delete(Object cache) {
        try {
          ((HttpResponseCache) cache).delete();
        } catch (IOException ignored) {
        }
      }
  }

之后,您需要添加


void clearDiskCache();

在Downloader.java中添加方法。然后,您需要在UrlConnectionDownloader.java和OkHttpDownloader.java中添加未实现的方法。您应该像这样在UrlConnectionDownloader.java中定义public void clearDiskCache()方法:

@Override
public void clearDiskCache() {
    // TODO Auto-generated method stub
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH && cache != null) {
          ResponseCacheIcs.delete(cache);
        }
}

然后你需要添加:

void clearDiskCache(){
      downloader.clearDiskCache();
  }

在Dispacher.java中添加方法。然后添加:


public void clearDiskCache(){
      dispatcher.clearDiskCache();
  }

Picasso.java中的方法。

太棒了!现在你可以在你的代码中调用clearDiskCache()方法。这是一个例子:

Picasso picasso = Picasso.with(TestActivity.this);

picasso.clearDiskCache();

picasso.setDebugging(true);
picasso.setIndicatorsEnabled(true);
picasso.setLoggingEnabled(true);

picasso.load(imageURL).into(imageView);

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