JB2
JB2

Reputation: 1607

Phonegap: Resolving content:// URI obtained from native file chooser

I am using the Cordova Filechooser plugin to select files from my Android device. The plugin returns a content:// URI (e.g. content://com.android.providers.media.documents/document/image%3A15756). I am making a call to resolveLocalFileSystemURI in order to be able to resolve the content URL and draw the image on a canvas. However for some reason the URI isn't being resolved properly.

E.g. The returned entry's fullPath is /com.android.providers.media.documents/document/image%3A15756 for the content URI content://com.android.providers.media.documents/document/image%3A15756

Any ideas? My code is as follows:

window.resolveLocalFileSystemURI(_this.target_image, function (fileEntry) {
            var img = new Image();
            alert(fileEntry.fullPath);
            img.src = URL.createObjectURL(fileEntry.fullPath);
            img.onload = function() {
                    combiner_context.drawImage(img, 0, 0);
                    combiner_context.putImage(0, img.height, _that.editor_img);


            };
        }, function () {
            alert('Could not load selected file. Please try again.');
        });

Upvotes: 4

Views: 4736

Answers (1)

Anu2g
Anu2g

Reputation: 164

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*/});
    });
}

Upvotes: 4

Related Questions