3

I am trying to figure out the best and the most efficient way of caching images to the disk, such that they would persist even after app is killed and re-launched in airplane mode. Consider the following use case:

  1. Open the app and get all images and display them in their respective ImageView's
  2. Kill the app
  3. Put device in air plane mode
  4. Open the app again.

I am trying to get the images to persist in an offline cache so that they can be displayed in the scenario mentioned above.

I went through documentation for picasso and glide and it wasn't exactly clear if their disk caching would work in this case.

Is there a way to do this using picasso or glide? I am trying to avoid having to write a custom implementation for storing this in SQLite etc.

Zoe
  • 27,060
  • 21
  • 118
  • 148
  • I believe Picasso has offline network capabilities. In other words, it should be possible for Picasso to automatically fetch when online and save to disk for offline use. Check out: http://stackoverflow.com/a/29812750/394933 – Brian Apr 14 '17 at 18:02
  • Picasso offers no such hooks. You can set the proper cache headers from your server (or rewrite the responses in an OkHttp Interceptor) though. – Eric Cochran Apr 14 '17 at 19:35
  • @Brian is correct! Check this answer for details. http://stackoverflow.com/a/34051356/3921977 – Aishwarya Tiwari Apr 15 '17 at 01:03

1 Answers1

3

Glide will do this for you by default without any extra work on your side. You can also customize what versions of the requested image to store in the cache.

One important thing you need to consider is if the URLs that you use Glide to fetch images from are available offline otherwise you will need to have some way to cache those as well so that you can initiate the Glide calls when you are offline.

You can see how I did it in this project: https://github.com/KhalafMH/popular-movies-android.git

To read about how to configure Glide caching see:
http://bumptech.github.io/glide/doc/caching.html

Mahdi AlKhalaf
  • 101
  • 1
  • 1
  • 6