Phonegap相机错误

时间:2013-09-09 17:43:59

标签: javascript android cordova

我目前正想在相机关闭后直接拍摄照片,将照片添加为HTML图像的来源。但我不断收到以下错误:

09-09 19:16:07.764: E/System(10719): Uncaught exception thrown by finalizer
09-09 19:16:07.780: D/dalvikvm(10719): GC_CONCURRENT freed <1K, 10% free 19133K/21127K, paused 1ms+2ms
09-09 19:16:07.788: E/System(10719): java.lang.IllegalStateException: Binder has been finalized!
09-09 19:16:07.788: E/System(10719):    at android.os.BinderProxy.transact(Native Method)
09-09 19:16:07.788: E/System(10719):    at android.database.BulkCursorProxy.close(BulkCursorNative.java:288)
09-09 19:16:07.788: E/System(10719):    at android.database.BulkCursorToCursorAdaptor.close(BulkCursorToCursorAdaptor.java:133)
09-09 19:16:07.788: E/System(10719):    at android.database.CursorWrapper.close(CursorWrapper.java:49)
09-09 19:16:07.788: E/System(10719):    at android.content.ContentResolver$CursorWrapperInner.close(ContentResolver.java:1591)
09-09 19:16:07.788: E/System(10719):    at android.content.ContentResolver$CursorWrapperInner.finalize(ContentResolver.java:1604)
09-09 19:16:07.788: E/System(10719):    at java.lang.Daemons$FinalizerDaemon.doFinalize(Daemons.java:182)
09-09 19:16:07.788: E/System(10719):    at java.lang.Daemons$FinalizerDaemon.run(Daemons.java:168)
09-09 19:16:07.788: E/System(10719):    at java.lang.Thread.run(Thread.java:856)
09-09 19:16:08.585: D/dalvikvm(10719): GC_EXPLICIT freed 12354K, 68% free 6804K/21127K, paused 2ms+3ms
09-09 19:16:08.585: E/System(10719): Uncaught exception thrown by finalizer
09-09 19:16:08.585: E/System(10719): java.lang.IllegalStateException: Binder has been finalized!
09-09 19:16:08.585: E/System(10719):    at android.os.BinderProxy.transact(Native Method)
09-09 19:16:08.585: E/System(10719):    at android.database.BulkCursorProxy.close(BulkCursorNative.java:288)
09-09 19:16:08.585: E/System(10719):    at android.database.BulkCursorToCursorAdaptor.close(BulkCursorToCursorAdaptor.java:133)
09-09 19:16:08.585: E/System(10719):    at android.database.CursorWrapper.close(CursorWrapper.java:49)
09-09 19:16:08.585: E/System(10719):    at android.content.ContentResolver$CursorWrapperInner.close(ContentResolver.java:1591)
09-09 19:16:08.585: E/System(10719):    at android.content.ContentResolver$CursorWrapperInner.finalize(ContentResolver.java:1604)
09-09 19:16:08.585: E/System(10719):    at java.lang.Daemons$FinalizerDaemon.doFinalize(Daemons.java:182)
09-09 19:16:08.585: E/System(10719):    at java.lang.Daemons$FinalizerDaemon.run(Daemons.java:168)
09-09 19:16:08.585: E/System(10719):    at java.lang.Thread.run(Thread.java:856)
09-09 19:16:08.585: D/DroidGap(10719): Resuming the App

现在我使用的是Android 4.0.3和Cordova 2.3.0。我目前有以下代码:

HTML

<button onclick="capturePhoto();">Take Photo</button>
<img id="cameraPic" src="" style="width:auto;height:120px;"></img>

的JavaScript

var pictureSource; 
var destinationType;

 function onDeviceReady() {
        pictureSource=navigator.camera.PictureSourceType;
        destinationType=navigator.camera.DestinationType;
    }


function capturePhoto(){
    navigator.camera.getPicture(uploadPhoto,null,{ quality: 20, allowEdit: true,
        destinationType: destinationType.DATA_URL });
};

function uploadPhoto(data){
// this is where you would send the image file to server
//output image to screen
    $("#cameraPic").src = "data:image/jpeg;base64," + data;
}

我真的不确定为什么我会收到这个错误。任何帮助都会非常感激。

在我的config.xml文件中,我拥有以下权限:

<plugin name="Camera" value="org.apache.cordova.CameraLauncher"/>

在我的Android清单中,我有:

<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 

2 个答案:

答案 0 :(得分:2)

尝试使用以下代码。我猜你没有使用PhoneGap事件监听器调用onDeviceReady函数。

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

    document.addEventListener("deviceready",onDeviceReady,false);

    function onDeviceReady() {
        pictureSource=navigator.camera.PictureSourceType;
        destinationType=navigator.camera.DestinationType;
    }

    function capturePhoto() {
          navigator.camera.getPicture(uploadPhoto, uploadFail, { quality: 20, allowEdit: true,
            destinationType: destinationType.DATA_URL });
    }

    function uploadPhoto(data){
        $("#cameraPic").src = "data:image/jpeg;base64," + data;
    }

    function uploadFail(error) {
        alert('Failed because: ' + error);
    }

答案 1 :(得分:0)

检查目的地类型。

navigator.camera.getPicture(onSuccess, onFail, { quality: 50,
    destinationType: Camera.DestinationType.DATA_URL // check this line and replace it...
 }); 

function onSuccess(imageData) {
    var image = document.getElementById('myImage');
    image.src = "data:image/jpeg;base64," + imageData;
}

function onFail(message) {
    alert('Failed because: ' + message);
}