Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Load images from disk cache with Picasso if offline

Tags:

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.

like image 547
Daniele Vitali Avatar asked Apr 30 '14 14:04

Daniele Vitali


2 Answers

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);                         }                     }); 
like image 143
Hitesh Sahu Avatar answered Sep 18 '22 15:09

Hitesh Sahu


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

like image 31
João M Avatar answered Sep 19 '22 15:09

João M