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.