1

我有这段代码,它应该获取捕获图像的 base64,然后将其作为 jpg 保存到设备 SD 卡中,位于文件夹 MyAppFolder 下。但是它不起作用,我不知道为什么

<html>
<head>
<script src=../cordova.js></script>
<script>
// A button will call this function
//
function capturePhoto() {
sessionStorage.removeItem('imagepath');
// Take picture using device camera and retrieve image as base64-encoded string
navigator.camera.getPicture(onPhotoDataSuccess, onFail, { quality: 50, destinationType: Camera.DestinationType.FILE_URI });
}

function onPhotoDataSuccess(imageURI) { 
    // Uncomment to view the base64 encoded image data
    // console.log(imageData);

    // Get image handle
    //
    var imgProfile = document.getElementById('imgProfile');

    // Show the captured photo
    // The inline CSS rules are used to resize the image
    //
    imgProfile.src = imageURI;
    if(sessionStorage.isprofileimage==1){
        getLocation();
    }
    movePic(imageURI);
}

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

function movePic(file){ 
window.resolveLocalFileSystemURI(file, resolveOnSuccess, resOnError); 
} 

//Callback function when the file system uri has been resolved
function resolveOnSuccess(entry){ 
var d = new Date();
var n = d.getTime();
//new file name
var newFileName = n + ".jpg";
var myFolderApp = "MyAppFolder";

window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(fileSys) {      
//The folder is created if doesn't exist
fileSys.root.getDirectory( myFolderApp,
                {create:true, exclusive: false},
                function(directory) {
                    entry.moveTo(directory, newFileName,  successMove, resOnError);
                },
                resOnError);
                },
resOnError);
}

//Callback function when the file has been moved successfully - inserting the complete  path
function successMove(entry) {
//Store imagepath in session for future use
// like to store it in database
sessionStorage.setItem('imagepath', entry.fullPath);
}

function resOnError(error) {
alert(error.code);
}
</script>
</head>
<body>

<button onclick="capturePhoto()">Take photo</button>
<img src="" id="imgProfile" style=position:absolute;top:0%;left:0%;width:100%;height:100%;>


</body>
</html>

按下按钮时,相机不启动。

4

2 回答 2

1

我解决了它如下。它可能会帮助您:

function capturePhoto() {

// Retrieve image file location from specified source
navigator.camera.getPicture(onPhotoSuccess, function(message) {
    alert('Image Capture Failed');
}, {
    quality : 40,
    destinationType : Camera.DestinationType.FILE_URI
});

}

function onPhotoSuccess(imageURI) {

var gotFileEntry = function(fileEntry) {
    alert("Default Image Directory " + fileEntry.fullPath);
    var gotFileSystem = function(fileSystem) {

        fileSystem.root.getDirectory("MyAppFolder", {
            create : true
        }, function(dataDir) {
          var d = new Date();
          var n = d.getTime();
          //new file name
          var newFileName = n + ".jpg";
            // 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,
            onFail);
};
// resolve file system for image
window.resolveLocalFileSystemURI(imageURI, gotFileEntry, fsFail);

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

};

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

};
于 2013-09-30T05:34:53.687 回答
-1

该按钮不会被点击,因为您的图像src与其重叠。更改图像的位置,src您的代码应该可以正常工作

于 2014-04-28T15:49:54.940 回答