vsakos
vsakos

Reputation: 488

how to cache images?

I am writing an android app which will have an image feed, something like in for example the instagram app. My question is how can I cache these images so i dont get an out of memory exception?

I have read some tutorials, but all of them are caching Bitmaps in LruCache. I might be wrong but as I think the bitmap in the ImageView and a cached one use the same ammount of memory.

I'm thinking about storing the compressed images (for example JPEG) in an in-memory cache (and of course on the disk) and showing it only when it is visible on the screen, but then the CPU will eat up the battery as it will constantly clearing the ImageView when it's not visible and decompressing the image and showing it when it is in the viewport. And I'm not really sure that the scrolling will be lagless, even if I do it on a new thread.

An alternative is to do the same as I described above, but I wont remove the bitmap from the imageview immediately, only when there are a lot of images and i will run out of memory.

What do you think?

Upvotes: 2

Views: 6932

Answers (1)

vinitius
vinitius

Reputation: 3274

Here is an example step-by-step on how to cache images, in memory and disk:

http://androidexample.com/Download_Images_From_Web_And_Lazy_Load_In_ListView_-_Android_Example/index.php?view=article_discription&aid=112&aaid=134

But you can also use libs that already work pretty well like :

http://square.github.io/picasso/

The first link also contains explanation on how you should treat bitmaps to avoid outOfMemory.

Upvotes: 2

Related Questions