I recently ran into some OutOfMemory error in an android application I am working on, mostly because I am loading the images in their original size (now I know this is a bad idea).
Im now working on implementing methods to load scaled down versions of the images depending on the actual ImageView size and caching them as suggested in googles developer guide.
The guide states out very well how I should process and handle images loaded at runtime in Java code, but it leaves out how to work with images defined via XML in my layout files. For example if I have an ImageView with a predefined Image, the XML code would look something like this:
<ImageView
android:id="@+id/my_image_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/my_image"
/>
If I now want to replace that image at runtime, im checking the width and height of the ImageView, and load the new image according to these dimensions:
imageView.setImageBitmap(decodeSampledBitmapFromResource(Resources res, int resId,
int reqWidth, int reqHeight)
Obviously, this has not be done for the Image defined via XML. Do I have to worry about these images, too, or does the system handle the downscaling for them? Or should I avoid declaring the src of an imageview via XML at all?
Thanks in advance, danijoo
ImageViewdimensions, but I didn't find the point where the resources is loaded, yet. The answer is somewhere there. ;)