Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

phonegap resolveLocalFileSystemURL does not work for content uri on android

I want to receive share image from Android image gallery using Nexus 4 phone on Android version 5.1.1. I am using phonegap 4.2 and a phonegap WebIntent plugin github link and the phonegap file plugin doc link.

Text and links works fine but when I tried to share an image from the Android gallery, I get a URI like this:

content://com.google.android.apps.photos.contentprovider/0/1/content%3A%2F%2Fmedia%2Fexternal%2Fimages%2Fmedia%2F63131/ACTUAL

Instead of something like file:///....

I tried to use the window.resolveLocalFileSystemURL function to resolve this url which returns successful with a fileEntry object. However, when I try to use it to read the file, I get an error with code 1000 (see below code and output).

Interestingly, if display this url via a img tag, I am able to see the image fine.

<img src="" id="test_intent_image">
$('#test_intent_image').attr("src",uri);

This suggests the problem might be the window.resolveLocalFileSystemURL function which can't handle content://... type uris properly?

===== reference info ======

Here is the code I'm using (note: I tried this with other file functions such as copyTo() to a app folder but it gives the same error):

    document.addEventListener("deviceready", shared, false);
    document.addEventListener("resume", shared, false);


 function shared () {

window.plugins.webintent.getExtra(window.plugins.webintent.EXTRA_STREAM,
function(data) {
// data is the value of EXTRA_TEXT
    if (! data) {
        return;
    }

    window.resolveLocalFileSystemURL(
        data, 
        function (entry) {
            console.log('file entry: ' + JSON.stringify(entry, null,3) );

            function win(file) {
                var reader = new FileReader();
                reader.onloadend = function (evt) {
                    console.log("file read success");
                    console.log(evt.target.result);
                };

                reader.readAsText(file);
            };

            function fail (error) {
                console.log("file read error: " + error.code);
            };

            entry.file(win, fail);

        }, 
        function (error) {
            console.log('file error: ' + error);
        });

}, function() {
    // There was no extra supplied.
    // debug_log("web intent getExtra: no extra");
    }
);

}

Here is the console output of the code when I tried to share an image from the image gallery:

handle share stream :"content://com.google.android.apps.photos.contentprovider/0/1/content%3A%2F%2Fmedia%2Fexternal%2Fimages%2Fmedia%2F63087/ACTUAL"
file entry: {
   "isFile": true,
   "isDirectory": false,
   "name": "ACTUAL",
   "fullPath": "/com.google.android.apps.photos.contentprovider/0/1/content%3A//media/external/images/media/63087/ACTUAL",
   "filesystem": "<FileSystem: content>",
   "nativeURL": "content://com.google.android.apps.photos.contentprovider/0/1/content%3A%2F%2Fmedia%2Fexternal%2Fimages%2Fmedia%2F63087/ACTUAL"
}
file read error: 1000

Here is the intent filter in the AndroidManifest.xml file. Note: the sharing works fine, just not able to resolve URL so this is probably not an issue.

<intent-filter>
<action android:name="android.intent.action.SEND" />
            <category android:name="android.intent.category.DEFAULT" />
            <data android:mimeType="*/*" />
        </intent-filter>
like image 345
MicroApper Avatar asked Sep 28 '22 14:09

MicroApper


1 Answers

I have been having the exact same issue, with no direct solution using the standard file plugin.

I tried this plugin however https://github.com/hiddentao/cordova-plugin-filepath which is only for android content uri's.

You use this function instead, and it appears to be working.

window.FilePath.resolveNativePath('content://...', successCallback, errorCallback);

like image 156
Matt Way Avatar answered Sep 30 '22 09:09

Matt Way