如果离线,使用 Picasso 从磁盘缓存加载图像 [英] Load images from disk cache with Picasso if offline

查看:24
本文介绍了如果离线,使用 Picasso 从磁盘缓存加载图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当应用程序启动时,我从不同的网站下载了一些图像,方法是:

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.

推荐答案

您可以通过此策略使用此代码 Picasso 将在缓存中查找图像,如果失败则仅通过网络下载图像.

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

这篇关于如果离线,使用 Picasso 从磁盘缓存加载图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆