In my application I am going to be loading and displaying various images from a server, and there’s no limit to how big each one can be. I’ve already fought my battles with the various issues of Bitmap memory usage in Android that so many people have complained about here, and I’ve got that much working in that old bitmaps are being released and recycled when I’m done with them. My issue now is the possibility of a single enormous image exceeding the memory allocation all by itself. I’ve already looked into the various options for downsizing images to conserve memory and get how all that works – my issue is that I would like to preserve image quality as much as possible, so I would like the Bitmap to use as much memory as it can without killing everything.
So, my question is, given that there is such a wide variety of devices with different memory capacities, is there any way to determine a suitable maximum size at runtime in order to balance memory allocation with image quality?
I found myself with similar issues. After some research and testing I came up with a number of methods that helped me on that subject. Those are implemented with C# using Mono for Android but I guess they should be almost identical with Java:
That code proved pretty reliable preventing out of memory exceptions. An example using those methods in the namespace called Utils is the code snippet below. This code calculates the memory necessary for 3 bitmaps, two of them being 3 times as big as the first one.