简体   繁体   中英

navigator.camera.getPicture not working properly

In my cordova(v3.3) single page application use the following code to get the camera image

function takeSkiImage(){
   capturePhoto();
}  

function capturePhoto() {
// Take picture using device camera and retrieve image as base64-encoded string
    alert((navigator.camera.getPicture));
    navigator.camera.cleanup(); 
    navigator.camera.getPicture(onPhotoDataSuccess, function fail(error){
       alert("failed : " + error.code);
    }, {
      quality : 90,
      targetWidth : 2300,
      targetHeight : 1800,
      destinationType : Camera.DestinationType.FILE_URI
  });
}


function onPhotoDataSuccess(imageURI) {
    var gotFileEntry = function(fileEntry) {
    alert("got image file entry: " + fileEntry.fullPath);
    var gotFileSystem = function(fileSystem) {

        fileSystem.root.getDirectory("sample", {
            create : true
        }, function(dataDir) {
            var d = new Date();
            var n = d.getTime();
            var newFileName = n + ".jpg";
            alert("File Downloaded");
            // copy the file
            fileEntry.moveTo(dataDir, newFileName, null, fsFail);

        }, dirFail);

    };
    // get file system to copy or move image file to
    window.requestFileSystem(LocalFileSystem.PERSISTENT, 0,
            gotFileSystem, fsFail);
};
// resolve file system for image
window.resolveLocalFileSystemURI(imageURI, gotFileEntry, fsFail);

// file system fail
var fsFail = function(error) {
    alert("failed with error code: " + error.code);

};

var dirFail = function(error) {
    alert("Directory error code: " + error.code);

};
}

The above code working fine in NEXUS 7(v 4.2) device and alerts the camera start but in samsung tab 4(v 4.4) devices the alert camera start never fires first time but it goes to camera and able to take picture but not able to save.

when again take the new image at the time only the old image get stored. like wise take new image only the previous image stored. How to solve this. Any help is highly appreciable.

To save an image you need to configure options properly.

function takeSkiImage(){
   alert("hi");
   navigator.camera.getPicture(function(imageData) {
      alert("camera start");
   }, onFail, {
    quality : 100, allowEdit : true,
    targetWidth: 2350,
    targetHeight: 1800,
    destinationType : Camera.DestinationType.FILE_URI,
    saveToPhotoAlbum : true
   });
} 

The saveToPhotoAlbum : true is required to save image.

Update:

To save file in a custom location, you have to use File plugin .

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