Marco Cuccagna
Marco Cuccagna

Reputation: 105

Take a Picture and Save it in a Specific Folder PhoneGap

I'm developing an Android application with PhoneGap, my problem is I want to take a picture and save in a specified folder, I read a lot of tutorial online but I cannot find the solution on my problem, now I take the picture and save it in the default folder.

here is my js file.

var pictureSource;   
var destinationType;
document.addEventListener("deviceready",onDeviceReady,false);

function onDeviceReady() {
   pictureSource=navigator.camera.PictureSourceType;
   destinationType=navigator.camera.DestinationType;
}

function onPhotoDataSuccess(imageURI) {

   var smallImage = document.getElementById('smallImage');
   smallImage.style.display = 'block';
   smallImage.src = imageURI;
   movePic(imageURI);
}

function capturePhoto() {
   navigator.camera.getPicture(onPhotoDataSuccess, onFail, { quality: 50,
   destinationType: destinationType.FILE_URI,
   saveToPhotoAlbum: true});
}

function onFail(message) {
   alert('Failed because: ' + message);
}

function movePic(file){
   window.resolveLocalFileSystemURI(file, resolveOnSuccess, resOnError);
}

function resolveOnSuccess(entry){
   var d = new Date();
   var n = d.getTime();

   var newFileName = n + ".jpg";
   var myFolderApp = "Geofolder";

   window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(fileSys)  {  
        //The folder is created if doesn't exist
    var direct = fileSys.root;
          direct.getDirectory( myFolderApp,
            {create:true, exclusive: false},
            function(myFolderApp) {
                entry.moveTo(myFolderApp, newFileName,  successMove,  resOnError);
            },
            resOnError);
    },
    resOnError);
}

function successMove(entry) {
   sessionStorage.setItem('imagepath', entry.fullPath);

}

function resOnError(error) {
   alert(error.code); 

Upvotes: 1

Views: 1234

Answers (1)

Homen
Homen

Reputation: 1212

You have to get the directory entry.Try the following function.

function moveFile(fileUri) {
window.resolveLocalFileSystemURL(
      fileUri,
      function(fileEntry){
            window.resolveLocalFileSystemURL(cordova.file.externalRootDirectory,
                function(dirEntry) {
                        fileEntry.moveTo(dirEntry, "fileName.jpg", function(entry){
                            alert("File moved.check internal memory");
                        },resOnError);
                    },
                    resOnError);
      },
      resOnError);}

Upvotes: 2

Related Questions