Search code examples
androidcordovaphonegap-pluginsphonegap

PhoneGap - How to make a copy of image and save in application folder?


In my app the user can choose one image from gallery, this image will be used in user account and can't be accessible by gallery, so I need sabe a copy of this image into cordova.file.dataDirectory but I don't know how to get local file URI to make copy and save.

How to copy image file from local device to cordova.file.dataDirectory?

My app have a screen where user can give title and choose image using a input file.

I'm using cordova plugin file


Solution

  • Searching I found solution in old documentation of PhoneGap (recent docs no have this information). To copy files just use copyTo method.

        window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(entry){
            var parent = document.getElementById('parent').value;
            parentEntry = new DirectoryEntry({fullPath: parent});
    
            // copy the file to a new directory and rename it
            entry.copyTo(parentEntry, "file.copy", function(entry){
                console.log("New Path: " + entry.fullPath);
            }, function(error){
                console.log(error.code);
            });
    
        }, null);