检索数据时Phonegap相机故障

时间:2011-10-13 06:44:54

标签: android camera cordova

---------------更新---------------

似乎HTC Desire相机和此版本的Phonegap存在某种问题。问题似乎是HTC在Android上返回相机规格的方式。

---------------更新---------------

我一直在使用phonegap来重写我以前的应用程序的代码,但不是我需要使用相机API。当我在真实的手机上使用它时,它在拍摄照片后崩溃了,我在SendLog中遇到了这个错误。

10-07 09:44:46.980 D/AndroidRuntime( 1626): Shutting down VM
10-07 09:44:46.980 W/dalvikvm( 1626): threadid=1: thread exiting with uncaught exception (group=0x400259f8)
10-07 09:44:46.989 W/CameraThread( 1159): Release Camera - set mIsLastCameraClosed to true
10-07 09:44:46.989 W/CameraThread( 1159): CameraHandler Message - CLOSE_CAMERA end
10-07 09:44:46.989 E/AndroidRuntime( 1626): FATAL EXCEPTION: main
10-07 09:44:46.989 E/AndroidRuntime( 1626): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=33, result=-1, data=Intent { act=inline-data (has extras) }} to activity {com.theinit.example/com.theinit.example.TestPhoneGapActivity}: java.lang.NullPointerException
10-07 09:44:46.989 E/AndroidRuntime( 1626):     at android.app.ActivityThread.deliverResults(ActivityThread.java:3734)
10-07 09:44:46.989 E/AndroidRuntime( 1626):     at android.app.ActivityThread.handleSendResult(ActivityThread.java:3776)
10-07 09:44:46.989 E/AndroidRuntime( 1626):     at android.app.ActivityThread.access$2800(ActivityThread.java:135)
10-07 09:44:46.989 E/AndroidRuntime( 1626):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2166)
10-07 09:44:46.989 E/AndroidRuntime( 1626):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-07 09:44:46.989 E/AndroidRuntime( 1626):     at android.os.Looper.loop(Looper.java:144)
10-07 09:44:46.989 E/AndroidRuntime( 1626):     at android.app.ActivityThread.main(ActivityThread.java:4937)
10-07 09:44:46.989 E/AndroidRuntime( 1626):     at java.lang.reflect.Method.invokeNative(Native Method)
10-07 09:44:46.989 E/AndroidRuntime( 1626):     at java.lang.reflect.Method.invoke(Method.java:521)
10-07 09:44:46.989 E/AndroidRuntime( 1626):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
10-07 09:44:46.989 E/AndroidRuntime( 1626):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
10-07 09:44:46.989 E/AndroidRuntime( 1626):     at dalvik.system.NativeStart.main(Native Method)
10-07 09:44:46.989 E/AndroidRuntime( 1626): Caused by: java.lang.NullPointerException
10-07 09:44:46.989 E/AndroidRuntime( 1626):     at android.content.ContentResolver.openInputStream(ContentResolver.java:286)
10-07 09:44:46.989 E/AndroidRuntime( 1626):     at com.phonegap.CameraLauncher.onActivityResult(CameraLauncher.java:248)
10-07 09:44:46.989 E/AndroidRuntime( 1626):     at com.phonegap.DroidGap.onActivityResult(DroidGap.java:1346)
10-07 09:44:46.989 E/AndroidRuntime( 1626):     at android.app.Activity.dispatchActivityResult(Activity.java:3931)
10-07 09:44:46.989 E/AndroidRuntime( 1626):     at android.app.ActivityThread.deliverResults(ActivityThread.java:3730)
10-07 09:44:46.989 E/AndroidRuntime( 1626):     ... 11 more

有谁知道为什么会这样?

这是我的Camera.js

这是我的Camera.js

var options = { quality : 75, 
      destinationType : Camera.DestinationType.DATA_URL, 
      sourceType : Camera.PictureSourceType.CAMERA, 
      allowEdit : true,
      targetWidth: 100,
      targetHeight: 100 };

function capturePhoto() {
    // Take picture using device camera and retrieve image as base64-encoded string
    navigator.camera.getPicture(onPhotoDataSuccess, onFail, options);
}

function onPhotoDataSuccess() {
    // Uncomment to view the base64 encoded image data
    var theHTML = '';
    theHTML = '<div id="info">CameraSuccess</div>';
    document.getElementById('main').innerHTML = theHTML;
}

function onFail(message) {
    // Called if something bad happens. 
    alert('Failed because: ' + message);
}

希望有所帮助

顺便感谢所有人

4 个答案:

答案 0 :(得分:4)

我刚刚在PhoneGap问题列表中帮助了一个有同样错误的人。我相信你错过了许可:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
来自AndroidManifest.xml文件的

。我们需要能够将捕获的图像写入.jpg文件。

答案 1 :(得分:2)

当我将AndroidManifest.xml中的minSdkVersion设置为7时,我遇到了同样的问题。当我使用minSdkVersion =“2”时,我没有遇到这个问题。

答案 2 :(得分:1)

---------------更新---------------

  

似乎HTC Desire相机和此版本的Phonegap存在某种问题。问题似乎是HTC在Android上返回相机规格的方式。

---------------更新---------------

这段代码对我来说很好,我在索引上只有.js对这段代码的引用和带有capturePhoto()函数的按钮。

希望任何人都可以解决他们的问题。

var pictureSource;   // picture source
var destinationType; // sets the format of returned value 

// Wait for PhoneGap to connect with the device
//
document.addEventListener("deviceready",onDeviceReady,false);

// PhoneGap 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 });
}

// 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 }); 
}

// 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);
}

答案 3 :(得分:1)

修改

最初我曾想过在我的手机上清除我的应用程序并重新安装它已经解决了这个问题。事实证明情况并非如此。我发现在尝试从相机中抓取图像时,存在使用Android垃圾收集删除PhoneGap应用程序的问题。在搜索了几个小时后,我最终使用的解决方案是使用foreground camera plugin。这个插件在应用程序内部创建了自己的摄像头,这样你就不必担心垃圾收集了。

不幸的是,它没有完全功能,并且用户无法使用大多数相机选项。它也只支持Cordova 2.4.0,这意味着我必须从2.7.0降级。这个解决方案适用于我当前的应用程序,希望在我写的下一个应用程序中有更好的解决方案。希望这有助于某人!