来自docs页面的相机示例在android 2.3.x上失败

时间:2013-05-16 09:34:36

标签: android cordova

我正在创建一个用于在Android手机中捕捉照片的phonegap应用。它适用于android 4.x.但不适用于Android 2.3.x

出现了这样的错误:

抱歉! 应用程序AppName(packagename)已意外停止。请再试一次

我已经按照提到的here创建了项目并使用了完整示例here

我的phonegep html页面是:


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

错误日志:

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

2 个答案:

答案 0 :(得分:2)

默认的Phonegap(Cordova)相机插件调用原生相机,这使得Android垃圾收集器可以杀死后台应用程序。这个插件可以避免你的应用程序进入后台并被垃圾收集器和其他应用程序杀死。我们使用了Phonegap源代码并对其进行了修改以避免此问题。此插件仅适用于文件URI。

Foreground Camera Plugin for Phonegap

答案 1 :(得分:0)

取代 destinationType.FILE_URI ,使用 Camera.DestinationType.FILE_URI

参见以下代码

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

它适用于2.3.x以及4.x

希望这有帮助。