【问题标题】:Clear Disk/SD Card Cache of Android's Picasso Image Library清除安卓毕加索图片库的磁盘/SD卡缓存
【发布时间】:2014-10-16 16:00:48
【问题描述】:

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

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

我还尝试使用以下方法加载图像时跳过缓存:Picasso.with(ctx).load(new File("/path/to/image")).skipMemoryCache().into(imageView)

但是这些方法都不起作用。

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

【问题讨论】:

标签: android image caching picasso


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    您可以阅读此条目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);
    

    【讨论】:

      猜你喜欢
      • 2014-04-27
      • 2015-03-27
      • 2015-05-07
      • 2016-06-18
      • 2016-08-10
      • 2023-04-05
      • 2015-02-14
      • 2014-04-10
      • 2018-02-05
      相关资源
      最近更新 更多