问题
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>
回答1:
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);
回答2:
There's an open bug on the Cordova site about this.
https://issues.apache.org/jira/browse/CB-7024
来源:https://stackoverflow.com/questions/30928824/phonegap-resolvelocalfilesystemurl-does-not-work-for-content-uri-on-android