如何将PhoneGap中捕获的图像移动到SD卡中的文件夹

时间:2012-03-28 08:56:27

标签: android cordova

我正在使用以下代码来显示图像base64数据以显示并上传到服务器。 但我想将此捕获的图像保存在sdcard文件夹中。请帮我这样做。

这对于我获取base64图像数据是必要的,因为服务器仅支持此格式。这就是我使用destinationType = 0(表示DATA_URL)的原因。我现在有base64图像数据但是如何将这些数据保存到SD卡?

uploadPhoto(isSourceCamera, onPhotoDataSuccess, onFail);

function uploadPhoto(isSourceCamera, onPhotoDataSuccess, onFail)
{
    pictureSource = navigator.camera.PictureSourceType;
    if (isSourceCamera)
    {
        //QUALITY MUST BE LOW TO AVOID MEMORY ISSUES ON IPHONE4 ! (and other low memory phones).
        //must resize to make it faster to upload and avoid failure with low memory phones.
        navigator.camera.getPicture(onSuccessFunc, onFail, {
                            quality : 35,
                            sourceType : pictureSource.CAMERA,
                            targetWidth:750,
                            targetHeight:750,
                            allowEdit:true,
                            destinationType:0
                            });
    }
    else
    {
        navigator.camera.getPicture(onSuccessFunc, onFail, {
                            quality : 35,
                            sourceType : pictureSource.PHOTOLIBRARY,
                            targetWidth:750,
                            targetHeight:750,
                            allowEdit:true,
                            destinationType:0
                            });
    }
}

function onPhotoDataSuccess(imageData) 
{
     **//I want to smae my image here in sdcard folder.**
    var nodeid = localStorage.getItem("user_nodeid");
    var modifyImgData = imageData.replace(' ', '+');
    document.getElementById('image').src = setLocalStorageImage(localStorage.getItem(nodeid+"image"), modifyImgData);
    document.getElementById('profileMenu').src = setLocalStorageImage(localStorage.getItem(nodeid+"smallimage"), modifyImgData);
    $.ajax({
           type: "POST",
           url: appURL+"api/upload/image/" +nodeid+ "/1",
           data: "image=" + encodeURIComponent(modifyImgData),
           success: function(msg){
                    //No need to do anything here
                    if (msg.documentElement.getElementsByTagName("message")[0].childNodes[0].nodeValue != 'success')
                        onFail('Error in uploading image at server. Please try again.');
           }
        });
}

function onFail(message){
    alert(message);
}

3 个答案:

答案 0 :(得分:2)

以下是原始问题的正确答案: 如何将PhoneGap中捕获的图像移动到SD卡中的文件夹?

function onfail(error,caller){
    error = error || '[error]';
    caller = caller || '[caller]';
    alert('Error > '+caller+" code: "+error.code);
};

/*
    Error codes
    NOT_FOUND_ERR = 1;
    SECURITY_ERR = 2;
    ABORT_ERR = 3;
    NOT_READABLE_ERR = 4;
    ENCODING_ERR = 5;
    NO_MODIFICATION_ALLOWED_ERR = 6;
    INVALID_STATE_ERR = 7;
    SYNTAX_ERR = 8;
    INVALID_MODIFICATION_ERR = 9;
    QUOTA_EXCEEDED_ERR = 10;
    TYPE_MISMATCH_ERR = 11;
    PATH_EXISTS_ERR = 12;
*/


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

}; //doCameraAPI

function getImageURI(imageURI) {

    //resolve file system for image to move.
    window.resolveLocalFileSystemURI(imageURI, gotFileEntry, function(error){onfail(error,'Get Target Image')}); 


    function gotFileEntry(targetImg) {  
        //alert("got image file entry: " + targetImg.name);     
        //now lets resolve the location of the destination folder
        window.resolveLocalFileSystemURI(POSTPATH, gotDestinationEntry, function(error){onfail(error,'Get Destination Dir')});
        function gotDestinationEntry(destination){
            // move the file 
            targetImg.moveTo(destination, targetImg.name, moveSuccess, function(error){onfail(error,'Move Image')}); 
                 alert('dest :'+destination.fullPath);
            };

    function moveSuccess(){
        alert('FILE MOVE SUCCESSFUL!');   
    };
}; //getImageURI

信用于:谷歌手机上的nbk。

答案 1 :(得分:0)

由于您拥有Base64格式的数据,因此您只需使用FileWriter将数据保存到磁盘即可。

答案 2 :(得分:0)

我认为您需要将图像捕获为FILE_URL,然后将图像保存到SD卡中,如上面Steven Benjamin所述。

然后您可以将base64 DATA_URL作为

进行检索
function readFile() { // button onclick function 
    var gotFileEntry = function(fileEntry) { 
        console.log("got image file entry: " +  fileEntry.fullPath); 
        fileEntry.file( function(file) {
            var reader = new FileReader();
            reader.onloadend = function(evt) {
                console.log("Read complete!");
                image64.value = evt.target.result;
            };
            reader.readAsDataURL(file);
        }, failFile);
    };
    window.resolveLocalFileSystemURI("file:///mnt/sdcard/test.jpg", gotFileEntryImage, function(){console.log("* * * onPhotoURISuccess" + failed);});  
}