Sunday, March 09, 2014

Android - load and display images in apps

One of the more challenging problems after my recent update of the Android SDK was that the app was throwing an out of memory error on startup, at loading images.

After some deeper analysis of the problem, it seems that images have to be loaded effectively, and memory efficiently.

My solution is: defined image views in the layouts should not reference the drawable resources in the configuration. It is more efficient to set the image views after the view is actually in use.

So remove all "android:src=@drawable/..." from the xml files, unless are used.
Set them in code, when they actually get used. And, make sure to load only the scaled size of image needed for the view.

Loading bitmaps in an appropriate scale also makes your app memory efficient. More here