Kousik
Kousik

Reputation: 22415

Camera example from the docs page fails on android 2.3.x

I am creating a phonegap app for captureing photo in android phones. Its working fine for android 4.x. but not working for android 2.3.x

got an error like this:

Sorry! The application AppName(packagename) has stopped unexpectedly.please try again

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

My phonegep html page is:


<!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

Upvotes: 4

Views: 1830

Answers (2)

Kousik
Kousik

Reputation: 22415

The default Phonegap (Cordova) Camera Plugin calls the native camera and this makes Android Garbage Collector to kill background applications. This plugin avoid your application to go background and be killed by Garbage Collector with other applications. We used the Phonegap source code and modified it to avoid this problem. This plugin works only with File URI.

Foreground Camera Plugin for Phonegap

Upvotes: 2

Amol Chakane
Amol Chakane

Reputation: 1511

Instead of destinationType.FILE_URI use Camera.DestinationType.FILE_URI

See 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.

Upvotes: 0

Related Questions