Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cordova Move File using the file url

How can I move a file using the URL I get from the Camera?

neither successCallback nor errorCallback is called by the function moveTo. Can anyone tell me what I am doing wrong and what a possible solution looks like?

function successCallback(entry) {
    console.log("New Path: " + entry.fullPath);
    alert("Success. New Path: " + entry.fullPath);
}

function errorCallback(error) {
    console.log("Error:" + error.code)
    alert(error.code);
}

// fileUri = file:///emu/0/android/cache/something.jpg
function moveFile(fileUri) {
    newFileUri  = cordova.file.dataDirectory + "images/";
    oldFileUri  = fileUri;
    fileExt     = "." + oldFileUri.split('.').pop();

    newFileName = guid("car") + fileExt;

    // move the file to a new directory and rename it
    fileUri.moveTo(cordova.file.dataDirectory, newFileName, successCallback, errorCallback);
}

I am using Cordova version 4.1.2 Also installed the Cordova File Plugin

like image 686
Scratch. Avatar asked Dec 05 '22 23:12

Scratch.


1 Answers

You're trying to call the function moveTo on a String.

moveTO is not a function of String but of fileEntry. So first thing you need to do is get a fileEntry from your URI.

For that you'll call window.resolveLocalFileSystemURL :

function moveFile(fileUri) {
    window.resolveLocalFileSystemURL(
          fileUri,
          function(fileEntry){
                newFileUri  = cordova.file.dataDirectory + "images/";
                oldFileUri  = fileUri;
                fileExt     = "." + oldFileUri.split('.').pop();

                newFileName = guid("car") + fileExt;
                window.resolveLocalFileSystemURL(newFileUri,
                        function(dirEntry) {
                            // move the file to a new directory and rename it
                            fileEntry.moveTo(dirEntry, newFileName, successCallback, errorCallback);
                        },
                        errorCallback);
          },
          errorCallback);
}
like image 115
QuickFix Avatar answered Dec 21 '22 05:12

QuickFix