2

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);
}
Ram
  • 121
  • 3
  • 7

3 Answers3

2

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.

eldarerathis
  • 35,455
  • 10
  • 90
  • 93
Steven Benjamin
  • 199
  • 1
  • 4
  • It's a bit convoluted is it not? Why can't it be implemented as a method with two parameters, fromURI and toURI. I'm not picking at your solution, just wondering why it would not be better to do it that way and indeed if it is possible? – Andrew S Jan 02 '13 at 17:59
0

I think you need to capture the image as FILE_URL, and save the image to sdcard first as mentioned by Steven Benjamin above.

Then you can retrive the base64 DATA_URL as

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);});  
}
smanandhar
  • 160
  • 1
  • 5
0

Since you have the data in Base64 format you can just use the FileWriter to save the data to disk.

Simon MacDonald
  • 23,253
  • 5
  • 58
  • 74
  • Hi Simon Thanks for reply, I tried "FileWriter" but not successed to save in perticular location of SDCard (like sdcard/myphotos). How to assign this path? Also dont know how to save base64 data as a valid image in phonegap? Please help.. – Ram Mar 30 '12 at 07:30
  • Here's a good example of using the FileWriter. http://docs.phonegap.com/en/1.5.0/phonegap_file_file.md.html#FileWriter_full_example – Simon MacDonald Apr 02 '12 at 20:54