0

I use zxing to encode a qr code and store it as a bitmap and then show it in ImageView. Since the image generation time is significant I'm planning to move it to a separate thread (AsyncTaskLoader will be fine I think).

The problem is - it's an image and I know that to avoid memory leaks one should never store a strong reference to it in an Activity.

So how would you do it? How to cache an image to survive config changes (phone rotation) and generally avoid generating it onCreate()?

Just point me in the right direction, please.

EDIT: Or maybe, if I use AsyncTaskLoader anyway, I should let it do its job and keep the Bitmap there? I mean, it's supposed to return the data it's loaded (generated in this case) and return it when needed, isn't it?

Michał Klimczak
  • 12,674
  • 8
  • 66
  • 99
  • you need to save the image say sd card or internal storage.And used the saved uri or bitmap if exist otherwise fetch from the url for first time and store it – Arpit Garg Apr 12 '12 at 17:27

1 Answers1

0

Well this a very common question:

Saving Android Activity state using Save Instance State Activity restart on rotation Android How to pass an object from one activity to another on Android

You must probably use a Parceable object. http://chmcguir.wordpress.com/2012/02/12/parcelable-how-to-pass-data-between-android-activities/

EDIT:

This is an official article on the subject. It uses onRetainNonConfigurationChange() to pass bitmaps

Photostream for instance extracts the bitmaps from the drawables and pass the bitmaps only, not the drawables. Finally, remember that onRetainNonConfigurationChange() should be used only to retain data that is expensive to load. Otherwise, keep it simple and let Android do everything.

http://developer.android.com/resources/articles/faster-screen-orientation-change.html

Community
  • 1
  • 1
Aron
  • 144
  • 2
  • 11
  • Well I know how to save application state and use Parcelables, in fact I use it a lot in my app. But as far as I know, it's not preferred solution for things like Bitmaps. Or am I wrong? Bitmap implements Parcelable, but would it be so easy?... – Michał Klimczak Apr 12 '12 at 17:35
  • @Michał K I updated my answer. – Aron Apr 12 '12 at 20:00
  • About memory leaks and such I think you were refering to the same problem many users has that the garbage collector is to slow and thus you must help it by recycle the bitmaps. http://stackoverflow.com/questions/1949066/java-lang-outofmemoryerror-bitmap-size-exceeds-vm-budget-android/10098486#10098486 – Aron Apr 12 '12 at 20:06