简体   繁体   中英

Cordova 3.4.0 navigator.camera.getPicture does not callback onSuccess or onFail for Android 4.3

I am using Cordova 3.4 with Camera Plugin ( https://github.com/apache/cordova-plugin-camera/blob/master/doc/index.md )

When I call

navigator.camera.getPicture(onSuccess, onFail, {
        quality: 75,
        destinationType: window.Camera.DestinationType.FILE_URI,
        sourceType: window.Camera.PictureSourceType.CAMERA,
        //allowEdit: true,
        //cameraDirection: window.Camera.Direction.FRONT,
        //encodingType: window.Camera.EncodingType.JPEG,
        //targetWidth: 100,
        //targetHeight: 100,
        //popoverOptions: window.CameraPopoverOptions,
        saveToPhotoAlbum: true
    });
function onSuccess(imageData) {
    alert(imageData);
}
function onFail(message) {
    alert('Failed because: ' + message);
}

this code works for Windows Phone 8.1 but does not work for Android 4.3 (Jelly Bean). When I step into code in eclipse I can see that it successfully saves photo under android temp directory but does not call JavaScript success or fail event on complete, that's why I cannot get image on android.

I both tried on Galaxy Note 2 real device and emulator and did not call onSuccess on both.

Is there any known issues or workaround for this problem?

Try this options:

destinationType: navigator.camera.DestinationType.FILE_URI
sourceType: source
mediaType: media

If this isn't working, let me suggest these options. They're working as deployed on 4.2.2 (Jellybean) android and 4.4.2 (Kitkat).

navigator.camera.getPicture(this.onPhotoDataSuccess, this.onFail, {
            quality: 50,
            destinationType: Camera.DestinationType.DATA_URL,
            sourceType: Camera.PictureSourceType.CAMERA

        });

//reading and appending the DOM

onPhotoDataSuccess(imageData) {
        var smallImage = document.getElementById('smallImage');
        smallImage.style.display = 'block';
        smallImage.src = "data:image/jpeg;base64," + imageData;
    }

This will return a base64 encoded image.

If it helps anyone, I had this very same issue. It turned out that I was calling "navigator.camera.cleanup()" in the Cordova "pause" event of the app (so it would clean up resources when the app was sent to the background). The problem here was that the camera sends the app to the background, so apparently calling cleanup was breaking things.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM