Android – Cordova 3.4.0: Camera.getPicture() returns encoded URI when selected from GALLERY

androidcordova

I am using Camera.getPicture() API to capture image or select image from GALLERY.
When I take the picture using camera, it returns me FileEntry having correct URL with filename and extension. But when I select a file from Gallery, it returns "FileEntry.fullPath" as /com.android.providers.media.documents/document/image%3A322
and sometimes /media/external/images/media/319

What I want is, I want to verify the file type supported(that is jpg/jpeg) and the actual filename.

Is there a way to get the Filename with extension, which is selected.

Thanks in advance.

Code Snippet:

        var data = {};
        if( type === CAMERA){
            data = {
                        quality: quality,
                        destinationType: FILE_URI,
                        encodingType: JPEG, targetWidth: 1200, targetHeight: 1200,
                        saveToPhotoAlbum: true
                    };
        }
        else
        {
            data = {
                        destinationType: FILE_URI,
                        sourceType: PHOTOLIBRARY,
                        mediaType: ALLMEDIA

                    };
        }


        navigator.camera.getPicture(
                successCallback, errorCallback, data
        );   

      //The success callback method is : 
       successCallback: function(imageURI, param)
       {
                 //HERE THE imageURI value is coming with different format if selected from GALLERY
                 window.resolveLocalFileSystemURI(imageURI, 
            function(fileEntry) {fileEntry.file(onSuccess,onError);},
                            function(evt) {onError.call(this,evt.target.error);} );

       }

Best Answer

I was able to convert from a "content://" URI to a "file://" URI using this plugin: https://www.npmjs.com/package/cordova-plugin-filepath.

After obtaining the "file://" URI, I'm then able to use Cordova's resolveLocalFileSystemURL() function.

Hope this helps.

if (fileUri.startsWith("content://")) {
    //We have a native file path (usually returned when a user gets a file from their Android gallery)
    //Let's convert to a fileUri that we can consume properly
    window.FilePath.resolveNativePath(fileUri, function(localFileUri) {
        window.resolveLocalFileSystemURL("file://" + localFileUri, function(fileEntry) {/*Do Something*/});
    });
}
Related Topic