Android Picasso图像库的清除磁盘/ SD卡缓存

Pac*_*ker 9 android caching image picasso

我正在使用Picasso从我的服务器加载图像.它工作正常,但我正在加载图像,并在以后更改它.但是Picasso将图像缓存在磁盘中的某个位置(我检查了SD卡,找不到Picasso存储的任何目录).

我尝试按照此问题的接受答案的建议删除缓存:在Picasso中使缓存无效

我还尝试使用以下命令跳过缓存:Picasso.with(ctx).load(新文件("/ path/to/image")).skipMemoryCache().into(imageView)

但这些方法都没有奏效.

感谢任何可以帮助我解决此问题的建议或提示.

Lib*_*bin 7

Picasso磁盘映像缓存在app内部缓存目录中.看看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;
}
Run Code Online (Sandbox Code Playgroud)

删除目录中的所有文件

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();
}
Run Code Online (Sandbox Code Playgroud)


小智 1

您可以阅读此条目/sf/answers/1327521191/以了解 picasso 磁盘缓存的工作原理。

因此,首先您必须static void delete(Object cache)在 ResponseCacheIcs 类中添加方法。该类在 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) {
        }
      }
  }
Run Code Online (Sandbox Code Playgroud)

之后你必须添加

void clearDiskCache();
Run Code Online (Sandbox Code Playgroud)

Downloader.java 中的方法。然后你必须在 UrlConnectionDownloader.java 和 OkHttpDownloader.java 中添加未实现的方法。您应该public void clearDiskCache()在 UrlConnectionDownloader.java 中定义方法,如下所示:

@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);
        }
}
Run Code Online (Sandbox Code Playgroud)

然后你必须添加:

void clearDiskCache(){
      downloader.clearDiskCache();
  }
Run Code Online (Sandbox Code Playgroud)

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

public void clearDiskCache(){
      dispatcher.clearDiskCache();
  }
Run Code Online (Sandbox Code Playgroud)

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);
Run Code Online (Sandbox Code Playgroud)