I am trying to write a simple app for android. I am trying to preload 7 jpg images each of size ~75KB and resoluton 768×480. When run on HTC Flyer it runs fine. When run on HTC Evo 3D it crashes. Apparently I am allocating too much memeory:
1658880-byte external allocation too large for this process.
Out of memory: Heap Size=6663KB, Allocated=4297KB, Bitmap Size=25278KB, Limit=32768KB
Trim info: Footprint=6663KB, Allowed Footprint=6663KB, Trimmed=452KB
VM won't let us allocate 1658880 bytes
Clamp target GC heap from 33.136MB to 32.000MB
which seems quite weird. Am I supposed to create smaller images? I seems unlikely given today’s high resolution screens. Am I suppose extend heap size? Well that’s only supported from Android 3.0. What is the proper solution?
Btw. code I used for preloading images is:
stringsImages = new Drawable[]{
getResources().getDrawable(R.drawable.strings0),
getResources().getDrawable(R.drawable.strings1),
getResources().getDrawable(R.drawable.strings2),
getResources().getDrawable(R.drawable.strings3),
getResources().getDrawable(R.drawable.strings4),
getResources().getDrawable(R.drawable.strings5),
getResources().getDrawable(R.drawable.strings6)
};
To be sure to avoid possible confusion in calculations I am also using ~5MB additionally for calculations on audio data.
If you are running out of memory, try sub-sampling each drawable. This decreases the size of the image to save memory. See the docs here to understand how that works. I’ve done this and saved memory and not ever really seen any quality difference. Try running this code on each image:
Then you can set each bitmap to an
ImageViewwithimageView.setImageBitmap(bitmap1);