Romans
Romans

Reputation: 55

Storing temporary images best practice

I am creating an app that downloads six most recent images(thumbnails) from server and on swipe (via Viewpager) downloads next six. On swipe back it shows previous six. What is the best practice to store them? Should I save them on sqlite database and remove after session or should I store them on heap? Currently I store them on heap and when 42 thumbnails are downloaded, it destroys first six.

Upvotes: 0

Views: 1214

Answers (1)

Dheeraj Vepakomma
Dheeraj Vepakomma

Reputation: 28767

It depends. It's a trade-off between heap memory & speed and the need for persistence.

But if you have to store images persistently, save them as individual files in the application's cache directory (either internal or external) instead of in SQLite database. SQLite is best suited to store structured data rather than binary blobs. If you have associated metadata, you can store the paths to the files along with the metadata in SQLite.

The files in the cache will be automatically deleted when the device runs low on storage. But read the documentation for caveats.

If you choose to cache in the heap, consider using LruCache. It's available in the support library for API levels < 11.

Of course, the best option would be to have both a small memory cache backed up by a larger file cache.

Upvotes: 1

Related Questions