我想使用 Android 版本 5.1.1 上的 Nexus 4 手机从 Android 图片库接收共享图像。我正在使用phonegap 4.2和phonegap WebIntent插件github链接 https://github.com/florentvaldelievre/virtualartifacts-webintent和phonegap文件插件doc link http://docs.phonegap.com/en/edge/cordova_file_file.md.html#LocalFileSystem.
文本和链接工作正常,但当我尝试共享 Android 图库中的图像时,我得到如下 URI:
内容://com.google.android.apps.photos.contentprovider/0/1/content%3A%2F%2Fmedia%2Fexternal%2Fimages%2Fmedia%2F63131/ACTUAL
而不是像 file:///....
我尝试使用 window.resolveLocalFileSystemURL 函数来解析此 url,该 url 返回成功并带有 fileEntry 对象。但是,当我尝试使用它来读取文件时,出现代码 1000 的错误(请参阅下面的代码和输出)。
有趣的是,如果通过 img 标签显示这个 url,我就能很好地看到图像。
<img src="" id="test_intent_image">
$('#test_intent_image').attr("src",uri);
这表明问题可能是 window.resolveLocalFileSystemURL 函数无法正确处理 content://... 输入 uris?
=====参考信息======
这是我正在使用的代码(注意:我尝试使用其他文件函数(例如 copyTo() 到应用程序文件夹),但它给出了相同的错误):
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");
}
);
}
这是当我尝试共享图像库中的图像时代码的控制台输出:
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
这是 AndroidManifest.xml 文件中的意图过滤器。注意:共享工作正常,只是无法解析 URL,所以这可能不是问题。
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="*/*" />
</intent-filter>