我使用下面的代碼獲取圖像的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);
}
嗨西蒙 感謝您的回覆,我想 「FileWriter的」,而不是在successed SD卡的perticular位置(如SD卡/ myphotos)保存。 如何分配此路徑? 也不知道如何將base64數據保存爲phonegap中的有效圖像? 請幫忙.. – Ram 2012-03-30 07:30:50
以下是使用FileWriter的一個很好的例子。 http://docs.phonegap.com/en/1.5.0/phonegap_file_file.md.html#FileWriter_full_example – 2012-04-02 20:54:41