Camera example from docs page not working on android 2.3.x

I am creating a phonegap application for capturing photos on Android phones. Its works fine for android 4.x. but does not work for android 2.3.x

there was an error:

Excuse me! AppName (packagename) stopped unexpectedly. Please try again.

I created the project as mentioned here and using the full example.

My html phonegep page:


<!DOCTYPE html> <html> <head> <title>Capture Photo</title> <script type="text/javascript" charset="utf-8" src="cordova-2.5.0.js"></script> <script type="text/javascript" charset="utf-8"> var pictureSource; // picture source var destinationType; // sets the format of returned value // Wait for Cordova to connect with the device // document.addEventListener("deviceready",onDeviceReady,false); // Cordova is ready to be used! // function onDeviceReady() { pictureSource=navigator.camera.PictureSourceType; destinationType=navigator.camera.DestinationType; } // Called when a photo is successfully retrieved // function onPhotoDataSuccess(imageData) { // Uncomment to view the base64 encoded image data // console.log(imageData); // Get image handle // var smallImage = document.getElementById('smallImage'); // Unhide image elements // smallImage.style.display = 'block'; // Show the captured photo // The inline CSS rules are used to resize the image // smallImage.src = "data:image/jpeg;base64," + imageData; } // Called when a photo is successfully retrieved // function onPhotoURISuccess(imageURI) { // Uncomment to view the image file URI // console.log(imageURI); // Get image handle // var largeImage = document.getElementById('largeImage'); // Unhide image elements // largeImage.style.display = 'block'; // Show the captured photo // The inline CSS rules are used to resize the image // largeImage.src = imageURI; } // A button will call this function // function capturePhoto() { // Take picture using device camera and retrieve image as base64-encoded string navigator.camera.getPicture(onPhotoDataSuccess, onFail, { quality: 50, destinationType: destinationType.DATA_URL }); } // A button will call this function // function capturePhotoEdit() { // Take picture using device camera, allow edit, and retrieve image as base64-encoded string navigator.camera.getPicture(onPhotoDataSuccess, onFail, { quality: 20, allowEdit: true, destinationType: destinationType.DATA_URL }); } // A button will call this function // function getPhoto(source) { // Retrieve image file location from specified source navigator.camera.getPicture(onPhotoURISuccess, onFail, { quality: 50, destinationType: destinationType.FILE_URI, sourceType: source }); } // Called if something bad happens. // function onFail(message) { alert('Failed because: ' + message); } </script> </head> <body> <button onclick="capturePhoto();">Capture Photo</button> <br> <button onclick="capturePhotoEdit();">Capture Editable Photo</button> <br> <button onclick="getPhoto(pictureSource.PHOTOLIBRARY);">From Photo Library</button><br> <button onclick="getPhoto(pictureSource.SAVEDPHOTOALBUM);">From Photo Album</button><br> <img style="display:none;width:60px;height:60px;" id="smallImage" src="" /> <img style="display:none;" id="largeImage" src="" /> </body> </html> 

Error Log:

 05-16 14:51:38.249: E/AndroidRuntime(13459): FATAL EXCEPTION: main 05-16 14:51:38.249: E/AndroidRuntime(13459): java.lang.RuntimeException: Unable to resume activity {com.name/package}: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=34, result=-1, data=Intent { (has extras) }} to activity {com.name/com.name.namedemo}: java.lang.NullPointerException 05-16 14:51:38.249: E/AndroidRuntime(13459): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2120) 05-16 14:51:38.249: E/AndroidRuntime(13459): at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2135) 05-16 14:51:38.249: E/AndroidRuntime(13459): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1668) 05-16 14:51:38.249: E/AndroidRuntime(13459): at android.app.ActivityThread.access$1500(ActivityThread.java:117) 05-16 14:51:38.249: E/AndroidRuntime(13459): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931) 05-16 14:51:38.249: E/AndroidRuntime(13459): at android.os.Handler.dispatchMessage(Handler.java:99) 05-16 14:51:38.249: E/AndroidRuntime(13459): at android.os.Looper.loop(Looper.java:130) 05-16 14:51:38.249: E/AndroidRuntime(13459): at android.app.ActivityThread.main(ActivityThread.java:3683) 05-16 14:51:38.249: E/AndroidRuntime(13459): at java.lang.reflect.Method.invokeNative(Native Method) 05-16 14:51:38.249: E/AndroidRuntime(13459): at java.lang.reflect.Method.invoke(Method.java:507) 05-16 14:51:38.249: E/AndroidRuntime(13459): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:875) 05-16 14:51:38.249: E/AndroidRuntime(13459): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:633) 05-16 14:51:38.249: E/AndroidRuntime(13459): at dalvik.system.NativeStart.main(Native Method) 05-16 14:51:38.249: E/AndroidRuntime(13459): Caused by: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=34, result=-1, data=Intent { (has extras) }} to activity {com.name/com.name.namedemo}: java.lang.NullPointerException 05-16 14:51:38.249: E/AndroidRuntime(13459): at android.app.ActivityThread.deliverResults(ActivityThread.java:2532) 05-16 14:51:38.249: E/AndroidRuntime(13459): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2107) 05-16 14:51:38.249: E/AndroidRuntime(13459): ... 12 more 05-16 14:51:38.249: E/AndroidRuntime(13459): Caused by: java.lang.NullPointerException 05-16 14:51:38.249: E/AndroidRuntime(13459): at org.apache.cordova.DroidGap.onActivityResult(DroidGap.java:849) 05-16 14:51:38.249: E/AndroidRuntime(13459): at android.app.Activity.dispatchActivityResult(Activity.java:3908) 05-16 14:51:38.249: E/AndroidRuntime(13459): at android.app.ActivityThread.deliverResults(ActivityThread.java:2528) 05-16 14:51:38.249: E/AndroidRuntime(13459): ... 13 more 05-16 14:51:39.039: W/webcore(13459): Can't get the viewWidth after the first layout 
+4
source share
2 answers

The Phonegap camera plugin (Cordova) by default calls its own camera, and this causes the Android Garbage Collector to kill background applications. This plugin does not allow your application to go in the background and be killed by the garbage collector in other applications. We used the source code of Phonegap and modified it to avoid this problem. This plugin only works with a file URI.

Telegram main camera plugin

+2
source

Instead of destinationType.FILE_URI use Camera.DestinationType.FILE_URI

See the following code

 navigator.camera.getPicture(onPhotoDataSuccess, onFail, { quality: 50, destinationType: Camera.DestinationType.FILE_URI }); 

It works for me on 2.3.x as well as on 4.x

Hope this helps.

0
source

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


All Articles