Phonegap/Cordova out of memory when taking photos

I was having an issue with phonegap where an application on a specific phone would randomly crash after selecting a photo for uploading.

I installed ACRA ( http://acra.ch/ ) so the phone can send a dump when it crashes.

This turned out to be quite useful. The dump contained this part of interest

STACK_TRACE => java.lang.OutOfMemoryError: (Heap Size=23267KB, Allocated=5377KB)
at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:719)
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:791)

The phone is running out of memory when it tries to manipulate images.

After some research it seems that Android likes to load the entire image into memory using a bitmap format to manipulate it. This includes changing the image quality.

I had the image quality set to 50% to reduce upload time. But in order to change the quality, the phone has to load it into memory, make the changes, then save it out again for upload.

So the quick and easy fix for this is to not do any image manipulation. Set the image quality to 100%, and make sure image rotation is set to false.

eg

function capturePhoto() {
	navigator.camera.getPicture(onPhotoDataSuccess, onFail, {
		quality: 100,
		encodingType: navigator.camera.EncodingType.JPEG,
		destinationType: destinationType.FILE_URI,
		correctOrientation: false,
		saveToPhotoAlbum: true
	});
}