4

我正在创建一个 phonegap 应用程序,用于在 android 手机中捕获照片。它适用于 android 4.x。但不适用于 android 2.3.x

出现这样的错误:

对不起!应用程序 AppName(packagename) 已意外停止。请重试

我已经创建了这里提到的项目,并使用了这里的完整示例。

我的 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
4

2 回答 2

2

默认的 Phonegap (Cordova) Camera Plugin 调用本机摄像头,这使得 Android 垃圾收集器可以杀死后台应用程序。此插件可避免您的应用程序进入后台并被垃圾收集器与其他应用程序一起杀死。我们使用了Phonegap 源代码并对其进行了修改以避免这个问题。此插件仅适用于文件 URI。

Phonegap 的前景摄像头插件

于 2013-05-20T13:00:35.440 回答
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

希望这可以帮助。

于 2013-05-20T08:13:41.417 回答