How I load a 16-bit image?

We have a try/catch block inside of our catch block, which is where our last fallback mechanism is invoked.  We attempt to force garbage collection reduce color quality to 16-bit color, and downsample the image.  If that fails, then the final catch block merely prints the exception to the Android log and we return null/nil.  It’s proven to work on all Android device except the Nexus One, where the JVM completely terminates the first time an OutOfMemory exception occurs.

Now, we do try to predict if the device has enough memory to load the image too, and if it doesn’t, then we attempt downsample the image before our first attempt to load the image.  But based on my experience, guessing how much memory is left for the app on Android has proven to be unreliable on some devices.  Some devices just flat out lie at how much heap memory that they have… or perhaps that is a more likely case on rooted Android devices where this value is wrongly hard coded.

In any case, the only way we can improve our image loading at this point is to load all images from C/C++ on Android, where the available memory is much much larger, and it would be faster too.  It would involve us using zlib to access the image files within the APK and libpng/libjpeg to load the image files.  It would pretty much be a full rewrite of our image loading code, which we’re not prepared to do at the moment, but we do recognize that we need to do this in the future.

Joshua, I am asking if the point where the JVM terminates has native code (ie: not Java) running or not.

But by the way, improving loading speeds would make us very happy, all our games are very memory intensive, and are horribly slow on Android, sometimes taking more than a minute to load… And it is one of the major reasons we dumped Corona once (and are still considering doing so again, as soon we find something that has decent support beside community our thousand-level price for support).

No, our code within the Java catch block does not call any native C/C++ code.  Although I imagine the Android BitmapFactory class, which we use to load bitmaps, calls upon native code.  So, it may just be a bad Android OS fork for the particular device that you are running… and with our Nexus One, because this issue does not happen on any other device we’ve tested on.  At least that’s my conclusion.

check the image loading codes c#, maybe it will help you.

Regarding that bug, we might be seeing the same thing. For us it’s a HTC Desire HD that throws the out of memory error popup when using expansion files. In our testing we found when not using an expansion file it worked.