Android 使毕加索中的缓存无效
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22016382/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Invalidate cache in Picasso
提问by Maarten
I load an image from disk using Picasso, e.g., Picasso.with(ctx).load(new File("/path/to/image")).into(imageView), but whenever I save a new image in that file, and refresh my ImageView, Picasso still has the bitmap cached.
我使用Picasso从磁盘加载图像,例如,,Picasso.with(ctx).load(new File("/path/to/image")).into(imageView)但是每当我在该文件中保存新图像并刷新我的 时ImageView,Picasso 仍然缓存了位图。
Is it possible to invalidate the cache in Picasso?
是否可以使毕加索中的缓存无效?
回答by mes
In the recent versions of Picasso, there is a new method for invalidate, without any workarounds, so I think that custom PicassoTools class mentioned earlier, is now obsolete in this case
在最近的 Picasso 版本中,有一个新的 invalidate 方法,没有任何解决方法,所以我认为前面提到的自定义 PicassoTools 类在这种情况下现在已经过时了
Picasso.with(getActivity()).invalidate(file);
回答by shalafi
Actually, based on your own answer, there is an easier way to do it without forking the library. Add this class to the com.squareup.picasso package.
实际上,根据您自己的答案,有一种更简单的方法可以在不分叉库的情况下进行。将此类添加到 com.squareup.picasso 包中。
package com.squareup.picasso;
public class PicassoTools {
public static void clearCache (Picasso p) {
p.cache.clear();
}
}
Because cache has package visibility, this util class can clear the cache for you. You just have to call it:
由于缓存具有包可见性,因此该实用程序类可以为您清除缓存。你只需要调用它:
PicassoTools.clearCache(Picasso.with(context));
回答by Виталий Ермолович
Abort memory cache and disk cache check by indicate memory policy by flag: emoryPolicy.NO_CACHE and NetworkPolicy.NO_CACHE as below code snippet:
中止内存缓存和磁盘缓存检查,通过标志指示内存策略:emoryPolicy.NO_CACHE 和 NetworkPolicy.NO_CACHE,如下代码片段:
mPicasso.with(mContext)
.load(url)
.memoryPolicy(MemoryPolicy.NO_CACHE )
.networkPolicy(NetworkPolicy.NO_CACHE)
.resize(512, 512)
.error(R.drawable.login)
.noFade()
.into(imageView);
回答by user3361741
Try to use:
尝试使用:
Picasso.with(ctx).load(new File("/path/to/image")).skipMemoryCache().into(imageView)
回答by NguyenDat
The order of search image in Picasso is: Memory cache -> Disk cache -> Network
Picasso中搜索图片的顺序是:内存缓存->磁盘缓存->网络
So there are few scenario we need to invalidate cache in Picasso:
所以我们需要在毕加索中使缓存无效的情况很少:
1.Invalidate memory cache:
1.使内存缓存无效:
- Usercase: When image already update in disk cache or remote host
Solution: Clear cache of Url, File, Uri if exist
mPicasso.with(appContext).invalidate(File); mPicasso.with(appContext).invalidate(Url); mPicasso.with(appContext).invalidate(Uri);
- 用户案例:当图像已在磁盘缓存或远程主机中更新时
解决方案:清除缓存中的 Url、File、Uri(如果存在)
mPicasso.with(appContext).invalidate(File); mPicasso.with(appContext).invalidate(Url); mPicasso.with(appContext).invalidate(Uri);
.
.
2.Invalidate memory cache and disk cacheOnline
2.Invalidate memory cache and disk cache Online
※note: Onlinemean update directly to ImageView
※注意:在线意味着直接更新到ImageView
User case: Image updated on remote host
Solution: Abort image on memory cache and disk cache then request image on remote host
mPicasso.with(appContext) .load(url) .memoryPolicy(MemoryPolicy.NO_CACHE ) .networkPolicy(NetworkPolicy.NO_CACHE) .into(imageView);-> Abort memory cache and disk cache
用户案例:在远程主机上更新图像
解决方案:中止内存缓存和磁盘缓存上的图像,然后在远程主机上请求图像
mPicasso.with(appContext) .load(url) .memoryPolicy(MemoryPolicy.NO_CACHE ) .networkPolicy(NetworkPolicy.NO_CACHE) .into(imageView);-> 中止内存缓存和磁盘缓存
.
.
3.Invalidate memory cache and disk cacheOffline
3.Invalidate memory cache and disk cache Offline
※ note: Offlinemean update not update to ImageView, just background fetch to using later
※注意:离线意味着更新不更新到ImageView,只是后台获取以供稍后使用
- User case: You know image on remote host updated, but only want to update cache only to using afterward (not update into image view)
Solution: fetch only
mPicasso.with(appContext) .load(url) .memoryPolicy(MemoryPolicy.NO_CACHE) .networkPolicy(NetworkPolicy.NO_CACHE) .fetch();
- 用户案例:您知道远程主机上的图像已更新,但只想更新缓存以供以后使用(不更新到图像视图)
解决方案:只取
mPicasso.with(appContext) .load(url) .memoryPolicy(MemoryPolicy.NO_CACHE) .networkPolicy(NetworkPolicy.NO_CACHE) .fetch();
※Note: Using fetch() is good but it also consume network resource, so please consider carefully, check scenario 4 in below for better solution
※注意:使用 fetch() 虽好,但也消耗网络资源,请慎重考虑,查看下面的场景 4 以获得更好的解决方案
4.Invalidate memory cache and disk cacheOfflineif disk cache is exist
4.Invalidate memory cache and disk cache如果存在磁盘缓存则离线
- User case: Only invalidate cache if already exist in disk cache
Solution: Should check disk by using parameter: NetworkPolicy.OFFLINE cache before fetch
mPicasso.with(appContext) .load(url) .memoryPolicy(MemoryPolicy.NO_CACHE) .networkPolicy(NetworkPolicy.OFFLINE) .fetch(new Callback() { @Override public void onSuccess() { //Success: mean disk cache exist -> should do actual fetch picasso.load(url).fetch(); } @Override public void onError() { //Failed: mean disk cache not exist } });
- 用户案例:仅当磁盘缓存中已存在时才使缓存无效
解决方案:应该使用参数检查磁盘:NetworkPolicy.OFFLINE cache before fetch
mPicasso.with(appContext) .load(url) .memoryPolicy(MemoryPolicy.NO_CACHE) .networkPolicy(NetworkPolicy.OFFLINE) .fetch(new Callback() { @Override public void onSuccess() { //Success: mean disk cache exist -> should do actual fetch picasso.load(url).fetch(); } @Override public void onError() { //Failed: mean disk cache not exist } });
Picasso is an amazing libs, I hope squareup will add more convenience API to manage cache in upcoming future.
Picasso 是一个了不起的库,我希望 squareup 在不久的将来添加更多方便的 API 来管理缓存。
回答by Lawrence Kesteloot
Another option is to delete the cache directory itself, for example on app startup:
另一种选择是删除缓存目录本身,例如在应用程序启动时:
deleteDirectoryTree(context.getCacheDir());
where:
在哪里:
/**
* Deletes a directory tree recursively.
*/
public static void deleteDirectoryTree(File fileOrDirectory) {
if (fileOrDirectory.isDirectory()) {
for (File child : fileOrDirectory.listFiles()) {
deleteDirectoryTree(child);
}
}
fileOrDirectory.delete();
}
That deletes the whole cache directory, which is fine if you want to simulate first-use of your app. If you only want to delete the Picasso cache, add "picasso-cache" to the path.
这会删除整个缓存目录,如果您想模拟应用程序的首次使用,这很好。如果只想删除 Picasso 缓存,请在路径中添加“picasso-cache”。
回答by Aerilys
What you can do if you want to delete all cache at once, is to create a custom Picasso.LruCache, and then use the clearmethod on it.
如果您想一次删除所有缓存,您可以做的是创建一个自定义Picasso.LruCache,然后clear在其上使用该方法。
Here is a sample:
这是一个示例:
Picasso.Builder builder = new Picasso.Builder(this);
LruCache picassoCache = new LruCache(this);
builder.memoryCache(picassoCache);
Picasso.setSingletonInstance(builder.build());
To clear the cache:
要清除缓存:
picassoCache.clear();
回答by Stanley Kou
You can clear image cache of picasso by setting your own cache and clear that. This code was tested on Picasso 2.5.0
您可以通过设置自己的缓存并清除它来清除 picasso 的图像缓存。此代码已在 Picasso 2.5.0 上测试
private Picasso picasso;
private LruCache picassoLruCache;
picassoLruCache = new LruCache(context);
// Set cache
picasso = new Picasso.Builder(context) //
.memoryCache(picassoLruCache) //
.build();
// Clear cache
picassoLruCache.clear();
回答by Jo?o Machete
Doesn't loop pretty, but this approach fixed my issue with cache and Picasso. Only use this when you want to invalidate the cache for a specific URL, this approach is slow and probably is not the correct way of doing but works for me.
循环不太好,但这种方法解决了我的缓存和毕加索问题。仅当您想让特定 URL 的缓存无效时才使用此方法,这种方法很慢,可能不是正确的做法,但对我有用。
String url = "http://www.blablabla.com/Raiders.jpg";
Picasso.with(this).invalidate(url);
Picasso.with(this)
.load(url)
.networkPolicy(
NetworkUtils.isConnected(this) ?
NetworkPolicy.NO_CACHE : NetworkPolicy.OFFLINE)
.resize(200, 200)
.centerCrop()
.placeholder(R.mipmap.ic_avatar)
.error(R.mipmap.ic_avatar)
.into(imageView);
回答by OrangeTree
A very important part is missing from the accepted answer here. I found the trick from here: http://blogs.candoerz.com/question/124660/android-image-cache-is-not-clearing-in-picasso.aspx
此处接受的答案中缺少一个非常重要的部分。我从这里找到了诀窍:http: //blogs.candoerz.com/question/124660/android-image-cache-is-not-clearing-in-picasso.aspx
Just calling the following line, wouldn't clear the cache of a photo when you use custom options like resize, center crop etc when displaying the original image.
仅调用以下行,当您在显示原始图像时使用调整大小、居中裁剪等自定义选项时,不会清除照片的缓存。
Picasso.with(getContext()).invalidate(file);
The solution:
解决方案:
When displaying the image, use stableKey()method.
显示图像时,使用stableKey()方法。
Picasso.with(getContext()).load(new File(fileUri))
.skipMemoryCache()
.placeholder(R.drawable.placeholder)
.stableKey(fileUri)
.into(imageview);
Then, you can clear the cache of this file later by calling this:
然后,您可以稍后通过调用以下方法清除此文件的缓存:
Picasso.with(getContext()).invalidate(fileUri);
Hope this will help.
希望这会有所帮助。

