Phonegap: portrait app crashes

I use jQuery Mobile and PhoneGap to create an application that allows users to take a snapshot, view it on the screen and then upload it to a server application that will eventually publish it on the website.

This works well if I take a photo in landscape mode, but the application crashes when I take a portrait photo. As you can see from the code snippet below, I use the correctOrientation parameter to try to orient the photo after taking it. Without this parameter, the application does not crash, but when it is displayed, the photo is in the wrong orientation.

Now I am testing this on a Sony XPERIA android phone.

Here is the code responsible for the job.

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 did a few searches and I don't see anyone having this problem. Any help is appreciated

+4
source share
2 answers

I would need to make sure your stack trace would be reliable, but this is probably an OutOfMemoryException exception. Android handles image manipulation very poorly. It basically needs to load the entire image into memory, where each pixel is 4 bytes, which means up to 40 megabytes for many modern telephone cameras.

My only suggestion right now is to try switching the quality to 100. I know this sounds contradictory, but it doesn't have much processing in the background.

You can read my blog post about some camera issues:

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

Also, if I see a stack trace, this will help.

+2
source

this thread may be a little old, but the problem is still there. I took a deeper look at the code for telephone plugins and noticed that the problem is that, according to Simon, the Android class does not handle image operations → loading a large bitmap using the Bitmapfactory.decodeStream method. However, if you do not need to resize the image (taking it from the gallery →, you can do it later, for example, on the server), and you want to keep the quality 100 and you don’t need to use the correct settings parameter and the destination type is FILE_URI. .just you just need the URI of the images to load it somewhere, for example, then you need to enable 3 options: targetWidth is set to -1, targetHeight is set to -1 and correctOrientation is 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 information about this in the plugin code (comment on line 394, cordova 2.8.0). I don’t know why its not in the documentation, though;)

0
source

Source: https://habr.com/ru/post/1442041/


All Articles