简体   繁体   English

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

[英]Clear Disk/SD Card Cache of Android's Picasso Image Library

I am using Picasso to load images from my server. 我正在使用Picasso从我的服务器加载图像。 It works fine, but I am loading an image, and changed it later. 它工作正常,但我正在加载图像,并在以后更改它。 But Picasso has the image cached somewhere in disk (I checked the SD card, and could not find any directory that Picasso is storing in). 但是Picasso将图像缓存在磁盘中的某个位置(我检查了SD卡,找不到Picasso存储的任何目录)。

I tried to remove the cache as suggested by the accepted answer to this question : Invalidate cache in Picasso 我尝试按照此问题的接受答案的建议删除缓存: 在Picasso中使缓存无效

I also tried skipping cache when loading images using : Picasso.with(ctx).load(new File("/path/to/image")).skipMemoryCache().into(imageView) 我还尝试使用以下命令跳过缓存:Picasso.with(ctx).load(新文件(“/ path / to / image”))。skipMemoryCache()。into(imageView)

But none of these methods are working. 但这些方法都没有奏效。

Thanks for any suggestion or hint that could help me pass this problem. 感谢任何可以帮助我解决此问题的建议或提示。

Picasso disk images are cached in app internal cache directory . Picasso磁盘映像缓存在app内部缓存目录中。 Look at the method createDefaultCacheDir here https://github.com/square/picasso/blob/master/picasso/src/main/java/com/squareup/picasso/Utils.java 请看这里的方法createDefaultCacheDir https://github.com/square/picasso/blob/master/picasso/src/main/java/com/squareup/picasso/Utils.java

You can clear all the images in getCacheDir/picasso-cache like this 您可以像这样清除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;
}

To delete all files in directory 删除目录中的所有文件

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

You can read this entry https://stackoverflow.com/a/18964588/2358095 to understand how picasso disk cache works. 您可以阅读此条目https://stackoverflow.com/a/18964588/2358095以了解毕加索磁盘缓存的工作原理。

So first of all you have to add static void delete(Object cache) method in ResponseCacheIcs class. 首先,您必须在ResponseCacheIcs类中添加static void delete(Object cache)方法。 This class is defined in UrlConnectionDownloader.java. 此类在UrlConnectionDownloader.java中定义。 It seems like that: 看起来像是这样的:

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) {
        }
      }
  }

After that you have to add 之后你必须添加

void clearDiskCache();

method in Downloader.java. Downloader.java中的方法。 Then you have to add unimplemented method in UrlConnectionDownloader.java and OkHttpDownloader.java. 然后你必须在UrlConnectionDownloader.java和OkHttpDownloader.java中添加未实现的方法。 You should define public void clearDiskCache() method in UrlConnectionDownloader.java like this: 您应该在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);
        }
}

Then you have to add: 然后你必须添加:

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

method in Dispacher.java. Dispacher.java中的方法。 And then add: 然后添加:

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

method in Picasso.java. Picasso.java中的方法。

Bingo!!! 答对了!!! Now you can call clearDiskCache() method in your code. 现在,您可以在代码中调用clearDiskCache()方法。 Here is an example: 这是一个例子:

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

picasso.clearDiskCache();

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM