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

时间:2014-08-22 18:07:35

标签: android image caching picasso

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

我尝试按照此问题的接受答案建议删除缓存:Invalidate cache in Picasso

我还尝试使用以下方法在加载图像时跳过缓存:Picasso.with(ctx).load(新文件(“/ path / to / image”))。skipMemoryCache()。into(imageView)

但这些方法都没有奏效。

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

2 个答案:

答案 0 :(得分: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;
}

删除目录

中的所有文件
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 :(得分:1)

您可以阅读此条目https://stackoverflow.com/a/18964588/2358095以了解毕加索磁盘缓存的工作原理。

首先,您必须在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);
相关问题