你可以阅读这篇文章
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() {
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);