4

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.');
        });
JB2
  • 1,587
  • 2
  • 24
  • 40
  • 1
    There is not necessarily a file that you can access. That plugin does not implement a "file chooser", despite its name. It implements a content chooser, using Android's `ACTION_GET_CONTENT`. What comes back (your `content://` value) is a `Uri`, that a native Java app would use to get an `InputStream` to read in the chosen content. There is no requirement that a `Uri` be a file that you can access, and the odds of it being a file that you can access decrease with each successive Android OS release. – CommonsWare Jan 29 '15 at 17:13
  • I see. The file accessed are from the gallery, which is accessible by my application...Any alternative plugin that you would recommend? – JB2 Jan 29 '15 at 19:48
  • Sorry, I do not have a recommendation for you. – CommonsWare Jan 29 '15 at 23:42
  • Resolved: http://stackoverflow.com/questions/26735864/invoke-native-file-browser-using-phonegap – JB2 Jan 30 '15 at 15:58

1 Answers1

4

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*/});
    });
}
Anu2g
  • 164
  • 1
  • 5
  • i tried and it is working, but i get a SECURITY_ERR if i try to copy or read the File. I want to get the Content of a File from Outlook on Android. – hydrococcus Sep 28 '17 at 12:35