Xera12
Xera12

Reputation: 221

How to permanently cache data (Images) loaded?

My app loads and display thumbnails of top movies from a server via API. I don't want to query server and reload it every-time, I use glide to display images from server url.

How do i implement this image caching so that app uses previously loaded data, even if its offline.

Is there any library or do i have to store the images using sqlite and retrieve it ?

Thank you

Upvotes: 0

Views: 70

Answers (1)

Moonbloom
Moonbloom

Reputation: 7918

If you use Glide to load the images, there is an extremely simple one-liner to cache all images.

Simple add '.diskCacheStrategy(DiskCacheStrategy.SOURCE)' to your Glide loading, and it'll cache the image, and use it even if you're offline.

So it could look like this:

Glide.with(context)
       .load(imageUrl)
       .diskCacheStrategy(DiskCacheStrategy.SOURCE)
       .into(target);

You should take a look at the introduction to Glide, on their official github repo. It mentions a lot of details about how it works.

Upvotes: 1

Related Questions