0

In the spirit of not reinventing the bicycle I'm wondering if there's an Android best practice approach (which I'm not finding through google, but I'm probably not articulating my search correctly) for keeping a defined amount of local storage allocated for downloaded bitmaps, which are kept around based on a most commonly utilized (rather than FILO or FIFO) algorithm.

So, what I'm going to be doing is grabbing an image if it's not already on the disk and storing it and marking it as "most recently needed". The next time it's used I'll be marking it as most recently needed again. Every time I do this I go through all the images and any image that has fallen beyond some threshold of recent utility will be deleted.

My algorithm also takes into account the reality that not all images are created equal and that there's a finite amount of space that I wish to allocate for image storage.

I'm not asking for "how to do this" (because I know how to do it)... I'm asking if there's already a prescribed approach that I should follow before I embark on my own custom implementation.

Yevgeny Simkin
  • 27,946
  • 39
  • 137
  • 236

2 Answers2

3

I think you need LruCahe. you can use UIL and all available caching mechanism or create your own with DiskLruCache. I think most of the libraries use LRU as an algorithm for caching images and I do not think any of them use LIFO or FIFO.

mmlooloo
  • 18,937
  • 5
  • 45
  • 64
2

I can recommend the Picasso library for Android. Features as per site:

  • Handling ImageView recycling and download cancelation in an adapter.
  • Complex image transformations with minimal memory use. Automatic
  • memory and disk caching.

I'm not 100% sure if this meets your requirements but it's worth to take a look at.

Trinimon
  • 13,839
  • 9
  • 44
  • 60
  • thanks. it's hard to tell from their docs which caching strategy they employ - it's imperative for me that as new images come in, the ones being removed are removed based on "least previously utilized" and I can't tell if that's what they're doing, or just a FIFO approach. Thanks though, looks like a good tool. – Yevgeny Simkin Feb 27 '15 at 18:45
  • 1
    As per API documentation Picasso offers a `LruCache` implementation; this nice post discusses _Picasso_ and _UIL_ pros and cons: http://stackoverflow.com/questions/19995007/local-image-caching-solution-for-android-square-picasso-vs-universal-image-load; here you find a nice `LruCache` example http://stackoverflow.com/questions/11623994/example-using-androids-lrucache – Trinimon Feb 27 '15 at 19:02
  • thanks very much. I'm going to give the green checkmark to the more general answer by mmlooloo, but I really appreciate learning about this library, so, I'm +1ing your answer as well. – Yevgeny Simkin Feb 27 '15 at 21:07