25

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.

Daniele Vitali
  • 3,848
  • 8
  • 48
  • 71

5 Answers5

38

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);
                        }
                    });
Hitesh Sahu
  • 41,955
  • 17
  • 205
  • 154
  • 1
    Perfect solution for someone who wants an image caching library! :) – Zain Apr 03 '17 at 12:31
  • 2
    No need to write this much :) You can specify multiple network policies. For example `.networkPolicy(NetworkPolicy.OFFLINE, NetworkPolicy.NO_CACHE)` should have a similar behaviour to your code – Alex Ionescu Dec 09 '18 at 00:30
10

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

Community
  • 1
  • 1
João M
  • 1,939
  • 21
  • 14
  • 5
    As stated on the API specification, the OFFLINE policy will force the load from disk, skipping network https://square.github.io/picasso/2.x/picasso/com/squareup/picasso/NetworkPolicy.html. So, if the image changes on server and I am online, will I never notice the change? – Nicolás Arias Mar 30 '17 at 17:34
7

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);
Tyler Carberry
  • 2,001
  • 1
  • 14
  • 10
Jordan Réjaud
  • 472
  • 8
  • 13
4

Is OkHttp and Okio present 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)

You have 2 options

  • include the mentioned dependencies (recommended)
  • specify the cache manually
martyglaubitz
  • 992
  • 10
  • 21
  • 1
    it tooks me 3 bloody days to realize that im not inculding those 2 dependencies with picasso , that was really stupid X( X( thank you for pointing that out :D – mhdjazmati Sep 21 '16 at 09:40
1

To avoid creating of separate instance of RequestCreator like here , do this:

 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);
                        }
                    });
NickUnuchek
  • 11,794
  • 12
  • 98
  • 138