如何在PhoneGap的移动拍摄的图像在SD卡的文件夹 [英] How to move captured image in PhoneGap to a folder in sdcard
问题描述
我使用以下code要获得图片的base64数据可显示并上传到服务器。
但我想在保存文件夹中的SD卡这个拍摄的图像。请帮我做到这一点。
这是必要的,我得到的base64图像数据,因为服务器只支持这种格式。这就是为什么我使用destinationType = 0(表示DATA_URL)。我现在的base64有图像数据,但我怎么能保存这些数据到SD卡?
uploadPhoto(isSourceCamera,onPhotoDataSuccess,onFail);功能uploadPhoto(isSourceCamera,onPhotoDataSuccess,onFail)
{
pictureSource = navigator.camera.PictureSourceType;
如果(isSourceCamera)
{
//质量一定要低,以避免内存问题ON IPHONE4! (和其他低内存的手机)。
//必须调整,使之更快地上传和避免与低内存的手机的失败。
navigator.camera.getPicture(onSuccessFunc,onFail,{
品质:35,
sourceType的:pictureSource.CAMERA,
targetWidth:750,
targetHeight:750,
allowEdit:真实,
destinationType:0
});
}
其他
{
navigator.camera.getPicture(onSuccessFunc,onFail,{
品质:35,
sourceType的:pictureSource.PHOTOLIBRARY,
targetWidth:750,
targetHeight:750,
allowEdit:真实,
destinationType:0
});
}
}功能onPhotoDataSuccess(为imageData)
{
** //我想在这里我SMAE图像中的SD卡文件夹中。**
变种NODEID = localStorage.getItem(user_nodeid);
变种modifyImgData = imageData.replace('','+');
。的document.getElementById('形象')SRC = setLocalStorageImage(localStorage.getItem(NODEID +图像),modifyImgData);
。的document.getElementById('profileMenu')的src = setLocalStorageImage(localStorage.getItem(NODEID +smallimage),modifyImgData);
$阿贾克斯({
键入:POST,
网址:appURL +API /上传/图片/+ NODEID +/ 1
数据:形象=EN + codeURIComponent(modifyImgData)
成功:函数(MSG){
//没有必要在这里做什么
如果(msg.documentElement.getElementsByTagName(消息)[0] .childNodes [0] .nodeValue!='成功')
onFail('错误在服务器上上传图片请再试一次。);
}
});
}功能onFail(消息){
警报(消息);
}
下面是正确的答案原来的问题:
如何在PhoneGap的移动拍摄的图像文件夹中的SD卡?
函数onfail(错误,来电){
错误=错误|| '[错误]';
来电显示主叫= || '[呼叫者]';
警报('错误>'+来电+code:+错误code);
};/ *
错误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;
* /
功能doCameraAPI(){
//从指定源中检索图像文件位置
navigator.camera.getPicture(getImageURI,功能(消息){
警报('图像捕捉失败');
},{
品质:40,
destinationType:Camera.DestinationType.FILE_URI
});}; // doCameraAPI功能getImageURI(imageURI){ //图像移动解决文件系统。
window.resolveLocalFileSystemURI(imageURI,gotFileEntry,功能(错误){onfail(错误,获取目标图像')});
功能gotFileEntry(targetImg){
//警报(得到的图像文件条目:+ targetImg.name);
//现在可以解决的目标文件夹的位置
window.resolveLocalFileSystemURI(POSTPATH,gotDestinationEntry,功能(错误){onfail(错误,让目标目录')});
功能gotDestinationEntry(目标){
//移动文件
targetImg.moveTo(目的地,targetImg.name,moveSuccess,功能(错误){onfail(错误,移动图片)});
警报('DEST:'+ destination.fullPath);
}; 功能moveSuccess(){
警报('文件移动成功了!');
};
}; // getImageURI
信贷:NBK对谷歌的PhoneGap组
I am using following code to get image base64 data to display and upload to server. But i want to save this captured image in sdcard folder. Please help me to do this.
This is necessary for me to get base64 image data because server support only this format. That's why i am using destinationType = 0 (means DATA_URL). I have base64 image data now but how can i save this data to sdcard?
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);
}
Here is the correct answer to the original question: How to move captured image in PhoneGap to a folder in sdcard?
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
credit to: nbk on the google phonegap group.
这篇关于如何在PhoneGap的移动拍摄的图像在SD卡的文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!