Android 如果离线,使用 Picasso 从磁盘缓存加载图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23391523/
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
Load images from disk cache with Picasso if offline
提问by Daniele Vitali
I have some images that I download from different web sites when the app starts, by doing this:
我在应用程序启动时从不同的网站下载了一些图像,方法如下:
Picasso.with(context).load(image_url).fetch();
Now, suppose the user closes the app and turns offline. When the app starts again, Picasso display the images in this way:
现在,假设用户关闭应用程序并离线。当应用程序再次启动时,毕加索以这种方式显示图像:
Picasso.with(ctx).load(image_url).placeholder(R.drawable.ph).into(imageView);
The problem is that some images are loaded from the disk cache (yellow triangle in debug mode), and for the others Picasso shows the placeholder.
问题是一些图像是从磁盘缓存加载的(调试模式下的黄色三角形),而对于其他图像,毕加索显示了占位符。
Why? I'm expecting that every image is loaded from the disk cache.
为什么?我期望从磁盘缓存中加载每个图像。
回答by Hitesh Sahu
You can use this code by this strategy Picasso will look for images in cache and if it failed only then image will be downloaded over network.
您可以通过此策略使用此代码 Picasso 将在缓存中查找图像,如果失败,则图像将通过网络下载。
Picasso.with(context)
.load(Uri.parse(getItem(position).getStoryBigThumbUrl()))
.networkPolicy(NetworkPolicy.OFFLINE)
.into(holder.storyBigThumb, new Callback() {
@Override
public void onSuccess() {
}
@Override
public void onError() {
// Try again online if cache failed
Picasso.with(context)
.load(Uri.parse(getItem(position)
.getStoryBigThumbUrl()))
.placeholder(R.drawable.user_placeholder)
.error(R.drawable.user_placeholder_error)
.into(holder.storyBigThumb);
}
});
回答by Jo?o Machete
Do this:
做这个:
Picasso.with(this)
.load(url)
.networkPolicy(NetworkPolicy.OFFLINE)
.into(imageView);
Also check my previous answer, maybe will help you: Invalidate cache in Picasso
另请查看我之前的答案,也许会对您有所帮助:使 毕加索中的缓存无效
回答by Jordan Réjaud
This logic worked for me:
这个逻辑对我有用:
if network is available:
Picasso.with(context).load(image).into(imageView);
else:
Picasso.with(context).load(image).networkPolicy(NetworkPolicy.OFFLINE).into(imageView);
回答by martyglaubitz
Is OkHttpand Okiopresent on the class path? (or in your dependencies) Because by default Picasso lets the HttpClient handle the caching (it does not do this by default)
是OkHttp和奥基奥出现在类路径?(或在您的依赖项中)因为默认情况下,毕加索让 HttpClient 处理缓存(默认情况下它不这样做)
You have 2 options
你有2个选择
- include the mentioned dependencies (recommended)
- specify the cache manually
- 包括提到的依赖项(推荐)
- 手动指定缓存
回答by NickUnuchek
To avoid creating of separate instance of RequestCreator
like here, do this:
为避免创建RequestCreator
like here的单独实例,请执行以下操作:
RequestCreator request = mPicasso.load(TextUtils.isEmpty(imageUrl) ? null : imageUrl)
.placeholder(R.drawable.ic_default)
.error(R.drawable.ic_default)
.transform(transformations)
.noFade()
.centerCrop();
request
.networkPolicy(NetworkPolicy.OFFLINE)
.into(mImageView, new Callback.EmptyCallback(){
@Override
public void onError() {
super.onError();
request.into(mImageView);
}
});