1

When my application connects to the server I check which icons are outdated or missing.
I store those icons in a sqllite database, the icons aren't very big, around between 5-10kb.

Everytime an activity starts, I'll go to the database and get the necessary icons.

But many of my activities use the same icons, so I was thinking about getting them from the database once, and then cache them somewhere to speed up my application.

How should I do this ?

I was reading this link: http://developer.android.com/training/displaying-bitmaps/cache-bitmap.html and they mentioned an lruCache for bitmaps.

But isn't this a bit of overkill for what I'm trying to achieve?

Robby Smet
  • 4,649
  • 8
  • 61
  • 104
  • You may use WeakReferences for images to load once from SDCard and then It will be loaded from run time memory [1]: http://stackoverflow.com/questions/9319713/android-fast-bitmap-loading – Atul Bhardwaj Oct 29 '12 at 12:47
  • There are some of the benefits of caching in this article, reduced network usage improves battery life and should make your app a bit quicker. http://developer.att.com/developer/forward.jsp?passedItemId=9700192 – Rod Burns Nov 05 '12 at 17:33

2 Answers2

2

Absolutely no. The LruCache is the proper approach to cache stuff, it doesn't matter the size of the images. Remember to create the cache as a singleton element to be accessed equally by any element on your whole application.

DO NOT USE WeakReference as some people will suggest. They do not work very well on the Android, if you're in a search mood you can check the Google IO 2012 talks on YouTube that the Android team themselves are explaining why not use WeakReference and how to properly use LruCache.

edit

found the youtube link http://www.youtube.com/watch?v=gbQb1PVjfqM&feature=plcp

Budius
  • 39,391
  • 16
  • 102
  • 144
1

Your question does not sound like you are asking how to do a memory cache, but whether it's reasonable to do it at all. Does the application already feel like the icon loading is a bottleneck? What else is your application doing? Some networking perhaps? How many activities (out of all activities) actually do have some icons? How many icons per activity do you have? How many icons is there in total? Guessing from @Budius post, doing memory cache right is not completely straightforward issue on Android, so make sure that you are not doing an unjustified premature optimization

Pavel Zdenek
  • 7,146
  • 1
  • 23
  • 38
  • Thx for the concern and advise. I just want to prevent uneccesary querys to the database since the icons are repeated throughout multiple activities. – Robby Smet Oct 29 '12 at 14:52
  • 1
    The decision you should make is, whether the querys are so much of a performance problem that they should be avoided at cost of implementing a cache which seems to be tricky to do 100% correctly, and eat your application memory. That's all what i wanted to say :) – Pavel Zdenek Oct 29 '12 at 15:34