2012-03-28 60 views
2

我使用下面的代码获取图像的base64数据显示并上传到服务器。 但我想将这个捕获的图像保存在SD卡文件夹中。请帮助我做到这一点。如何在PhoneGap的移动拍摄的图像文件夹中的SD卡

这对我来说需要获取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); 
} 

回答

0

既然你有Base64编码格式的数据,你可以只使用FileWriter将数据保存到磁盘。

+0

嗨西蒙 感谢您的回复,我想 “FileWriter的”,而不是在successed SD卡的perticular位置(如SD卡/ myphotos)保存。 如何分配此路径? 也不知道如何将base64数据保存为phonegap中的有效图像? 请帮忙.. – Ram 2012-03-30 07:30:50

+0

以下是使用FileWriter的一个很好的例子。 http://docs.phonegap.com/en/1.5.0/phonegap_file_file.md.html#FileWriter_full_example – 2012-04-02 20:54:41

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 

贷记:谷歌phonegap组上的nbk。

+0

这有点令人费解,不是吗?为什么它不能作为一个有两个参数的方法来实现,从URI和toURI。我没有选择你的解决方案,只是想知道为什么不这样做会更好,事实上如果可能的话? – 2013-01-02 17:59:47

+1

我得到window.resolveLocalFileSystemURI()错误。 – 2015-04-23 07:32:56

0

我认为你需要将图像捕捉为FILE_URL,并保存图像由史蒂芬本杰明上述到SD卡第一。

然后你可以retrive以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);}); 
} 
相关问题