4

I am using jQuery Mobile and PhoneGap to create an app that allows users to take a picture, view it on screen, and then upload it to a server application that will ultimately post it on a website.

This works well if I take the photo in Landscape mode, but crashes the application when I take a Portrait photo. As you can see from the code snippet below I am using the correctOrientation parameter to attempt to orient the photo after it is taken. Without this parameter the application doesn't crash, however the photo is in the wrong orientation when it gets displayed.

Right now I'm testing this on Sony XPERIA android phone.

Here is the code responsible for the work.

function onPhotoFail(message) {
    alert('Failed because: ' + message);
}

function onPhotoSuccess(imageURI) {             
    var $image = $("img#upload-image");
$image.attr("src", imageURI);
}

function capturePhoto() {
navigator.camera.getPicture(onPhotoSuccess, onPhotoFail, { quality: 50, destinationType: Camera.DestinationType.FILE_URI , correctOrientation: true });     
}

Here's the stack trace:

E/AndroidRuntime(31760): FATAL EXCEPTION: main
E/AndroidRuntime(31760): java.lang.OutOfMemoryError: bitmap size exceeds VM budget
E/AndroidRuntime(31760):        at android.graphics.Bitmap.nativeCreate(Native Method)
E/AndroidRuntime(31760):        at android.graphics.Bitmap.createBitmap(Bitmap.java:477)
E/AndroidRuntime(31760):        at android.graphics.Bitmap.createBitmap(Bitmap.java:444)
E/AndroidRuntime(31760):        at org.apache.cordova.CameraLauncher.getRotatedBitmap(CameraLauncher.java:483)
E/AndroidRuntime(31760):        at org.apache.cordova.CameraLauncher.onActivityResult(CameraLauncher.java:326)
E/AndroidRuntime(31760):        at org.apache.cordova.DroidGap.onActivityResult(DroidGap.java:823)
E/AndroidRuntime(31760):        at android.app.Activity.dispatchActivityResult(Activity.java:3908)
E/AndroidRuntime(31760):        at android.app.ActivityThread.deliverResults(ActivityThread.java:2549)
E/AndroidRuntime(31760):        at android.app.ActivityThread.handleSendResult(ActivityThread.java:2595)
E/AndroidRuntime(31760):        at android.app.ActivityThread.access$2000(ActivityThread.java:121)
E/AndroidRuntime(31760):        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:973)
E/AndroidRuntime(31760):        at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(31760):        at android.os.Looper.loop(Looper.java:130)
E/AndroidRuntime(31760):        at android.app.ActivityThread.main(ActivityThread.java:3701)
E/AndroidRuntime(31760):        at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(31760):        at java.lang.reflect.Method.invoke(Method.java:507)
E/AndroidRuntime(31760):        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
E/AndroidRuntime(31760):        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:624)
E/AndroidRuntime(31760):        at dalvik.system.NativeStart.main(Native Method)

I've done some searching around, and I don't see anybody having this exact problem. Any help is appreciated

Amit Kumar
  • 97
  • 7

2 Answers2

2

I would have to see your stack trace to be sure but it is probably an OutOfMemoryException. Android handles image manipulation very badly. It basically needs to load the entire image into memory where each pixel is 4 bytes, that means up to 40 megabytes for a lot of modern phone cameras.

My only suggestion right now is to try to switch the quality to 100. I know it sounds counter-intuitive but it make keep there from being as much processing in the background.

You can read my blog post talking about some of the problems with the Camera:

http://simonmacdonald.blogspot.ca/2012/07/change-to-camera-code-in-phonegap-190.html

Also, if I can see the stack trace it would help.

Simon MacDonald
  • 23,253
  • 5
  • 58
  • 74
  • thanks, but that didn't seem to do the trick. I edited the original message to add the stack trace. – Damien Steiger Oct 25 '12 at 17:15
  • Yeah, it is an out of memory exception. Loading the image into a Bitmap so it can be rotated to the proper orientation is taking too much memory. – Simon MacDonald Oct 25 '12 at 17:20
  • is there any way around that? If I specify the targetWidth and targetHeight, will that scale the image down before it tries to do the orientation? – Damien Steiger Oct 25 '12 at 17:56
  • Yes, it will load a scaled version of the image into the bitmap which would use less memory. – Simon MacDonald Oct 25 '12 at 18:04
  • Excellent. That did the trick. I changed the quality back to 50, and the targetHeight, targetWidth to 800. It was pretty dramatic file size difference, from about a meg down to about 25 K! I'll tweak the values to get the optimal quality and memory usage. Thanks. – Damien Steiger Oct 25 '12 at 18:20
0

this thread might be a little old but the issue is still out there. I've been looking more deeply into the phonegap plugins code and noticed that the problem is, as Simon said, with Android class not handling the image operations -> loading a big bitmap via Bitmapfactory.decodeStream method. However, if you dont need to resize the image (while taking it from gallery -> you can do it later, on serverside for example) and you want to preserve the 100 quality AND you don't need to use correctOrientation option AND your destination type is FILE_URI.. simply, you just need the images URI to upload it somewhere, for example, then you need to include 3 options: targetWidth set to -1, targetHeight set to -1 and correctOrientation set to false:

    navigator.camera.getPicture(cameraSuccess, cameraError, { quality: 100, targetWidth: -1, 
                    targetHeight: -1, correctOrientation: false,
                    destinationType: navigator.camera.DestinationType.FILE_URI,
                    sourceType: navigator.camera.PictureSourceType.SAVEDPHOTOALBUM});

You can find info on that in plugins code (comment on line 394, cordova 2.8.0). Dont know why its not in documentation though ;)

  • Maybe we should read the available memory and camera resolution and calculate (roughly) the needed memory space and adjust the image size accordingly - having the same problem, just an idea – Liron Harel Feb 15 '14 at 16:55