Home » Android » javascript – How would I get a File object from PhoneGap camera.getPicture?

javascript – How would I get a File object from PhoneGap camera.getPicture?

Posted by: admin June 15, 2020 Leave a comment

Questions:

This is probably simple and covered by some combination of functions in PhoneGap’s “Camera” plugin, “File” plugin, or “File-Transfer” plugin. I understand the user can select a file with:

navigator.camera.getPicture(function (fileURI) {

    // *** need help here ***

}, function ()
    // handle errors
}, {
    destinationType: window.Camera.DestinationType.FILE_URI,
    sourceType: window.Camera.PictureSourceType.PHOTOLIBRARY,
    mediaType: window.Camera.MediaType.ALLMEDIA
});

I can also change to destinationType: window.Camera.DestinationType.DATA_URL if that makes a difference.

My goal in the success handler is to get a File object (https://developer.mozilla.org/en-US/docs/Web/API/File).

How to&Answers:

Something like this should do it.

navigator.camera.getPicture(function (fileURI) {

    window.resolveLocalFileSystemURL(fileURI, 
        function(fileEntry){
            alert("got image file entry: " + fileEntry.fullPath);
            // fileEntry.file() should return a raw HTML File Object
        },
        function(){//error}
    );

}, function (){
// handle errors
}, {
    destinationType: window.Camera.DestinationType.FILE_URI,
    sourceType: window.Camera.PictureSourceType.PHOTOLIBRARY,
    mediaType: window.Camera.MediaType.ALLMEDIA
});

Answer:

window.resolveLocalFileSystemURI(fileURI, function(fileEntry) { /* your code here */ });