How to Implement My Own Disk Cache with Picasso Library - Android

How to implement my own disk cache with picasso library - Android?

Picasso uses the HTTP client for disk caching and if one is already configured it will use that instead of installing its own.

For the built-in UrlConnection the docs for installing a cache are here: https://developer.android.com/reference/android/net/http/HttpResponseCache.html

If you are using OkHttp then you just call setCache:
http://square.github.io/okhttp/2.x/okhttp/com/squareup/okhttp/OkHttpClient.html#setCache-com.squareup.okhttp.Cache-

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.

Android - use picasso to load image without storing it in cache

Picasso supports this by it's skipMemoryCache() in the Picasso builder. An example is shown below.

Picasso.with(context).load(imageUrl)
.error(R.drawable.error)
.placeholder(R.drawable.placeholder)
.skipMemoryCache()
.into(imageView);

With the new API you should use it like this so that it skips looking for it and storing it in the cache:

Picasso.with(context).load(imageUrl)
.error(R.drawable.error)
.placeholder(R.drawable.placeholder)
.memoryPolicy(MemoryPolicy.NO_CACHE, MemoryPolicy.NO_STORE)
.into(imageView);

NO_CACHE

Skips memory cache lookup when processing a request.

NO_STORE

Skips storing the final result into memory cache. Useful for one-off requests to avoid evicting other bitmaps from the cache.

Does picasso library caches images automatically?

1)Does Picasso caches images automatically?

Yes, it does. This is the description of the library:

A powerful image downloading and caching library for Android.

So you'll have your own cache.

Because I have seen people using okhttp3 to cache images for offline use.

There are many other libraries for that.

If yes why would they use another library to cache?

You can use Picasso or any other library but it's up to you to choose which one you feel more comfortable with.

2) if your phone storage is running low, would Picasso caching system still cache it and crash the app?

Yes, it will. This library is not checking your disk space. This operation should be done by the user. Eventually, when you have no more space left on your device and your app will try to write a new image to the disk, it will throw an exception.

3)In my app, i am using Picasso.get().fit(). If Picasso library is caching, does it cache the whole image or only the fit version(smaller than the whole image).

It will cache the image you get but there are options to transform it. You can resize the image as you like:

Picasso.get()
.load(url)
.resize(50, 50)
.centerCrop()
.into(imageView)

PS. For Android, we use also Glide:

Glide is a fast and efficient open-source media management and image loading framework for Android that wraps media decoding, memory and disk caching, and resource pooling into a simple and easy to use interface.



Related Topics



Leave a reply



Submit