How to Use Disk Caching in Picasso

Android picasso cache images

Picasso automatically caches the loaded images, So that next time they will be loaded from the cache. You can check whether the image is loaded from the web, cache or disk by enabling the indicator

setIndicatorsEnabled(true)

Sample Image

Indicators will be shown for each image, specifying where the image is loaded from.

I got the reference from here

Using Picasso with custom disk cache

Picasso doesn't have a disk cache. It delegates to whatever HTTP client you are using for that functionality (relying on HTTP cache semantics for cache control). Because of this, the behavior you seek comes for free.

The underlying HTTP client will only download an image over the network if one does not exist in its local cache (and that image isn't expired).

That said, you can create custom cache implementation for java.net.HttpUrlConnection (via ResponseCache or OkHttp (via ResponseCache or OkResponseCache) which stores files in the format you desire. I would strongly advise against this, however.

Let Picasso and the HTTP client do the work for you!

You can call setIndicatorsEnabled(true) on the Picasso instance to see an indicator from where images are being loaded. It looks like this:

Sample Image

If you never see a blue indicator, it's likely that your remote images do not include proper cache headers to enable caching to disk.

Picasso image caching

I've researched some more into your questions and decided that I should publish this as an answer rather than a comment.

  1. Yes - Picasso loads images asynchronously so making repeated calls will cause images to be downloaded in parallel.
  2. Yes - just make the call as normal and Picasso will handle the re-use of downloaded images e.g. in Activity1, call Picasso.with(this).load("image1"); and, later, make a call to the same URL in Activity2. The image will already be cached (either in memory or on device storage) and Picasso will re-use it, rather than downloading it again.
  3. Yes - see above (Picasso will automatically use cached images where available)
  4. This does not seem to have such a clear-cut answer. One thing you can do is provide an image to display if an error occurs while fetching the real image:

    Picasso.with(context)
    .load(url)
    .placeholder(R.drawable.user_placeholder)
    .error(R.drawable.user_placeholder_error)
    .into(imageView);

    The 'placeholder' will be displayed whilst the attempt is being made to fetch the image from the web; the 'error' image will be displayed, for instance, if the URL is not valid or if there is no Internet connection.

    Update, 17/03/2014:

    Picasso supports the use of a callback to report you of a failure. Modify your usual call (e.g. the above example) like so:

    .into(imageView, new Callback() {
    @Override
    public void onSuccess() {
    // TODO Auto-generated method stub
    }

    @Override
    public void onError() {
    // TODO Auto-generated method stub
    }
    });

In conclusion, it sounds like Picasso would be a great choice of library for you. It definitely makes image downloading very quick and very easy, so I like it a lot.

Picasso doesn't cache image on disk

Ah since this is happening when you change headers, you are most probably not setting the Cache-Control header

According to Jake wharton (One of the developer of Picasso)

Picasso doesn't have a disk cache. It delegates to whatever HTTP
client you are using for that functionality (relying on HTTP cache
semantics for cache control). Because of this, the behavior you seek
comes for free

Taken from Jake Wharton's answer here

Also,

If you never see a blue indicator, it's likely that your remote images
do not include proper cache headers to enable caching to disk



Related Topics



Leave a reply



Submit