我正在使用 Cordova Filechooser plugin从我的 Android 设备中选择文件。该插件返回一个 content://URI(例如 content://com.android.providers.media.documents/document/image%3A15756)。我正在调用 resolveLocalFileSystemURI 以便能够解析内容 URL 并在 Canvas 上绘制图像。但是由于某种原因,URI 没有被正确解析。
例如返回条目的完整路径是/com.android.providers.media.documents/document/image%3A15756 对于内容 URI content://com.android.providers.media.documents/document/image%3A15756
有什么想法吗?我的代码如下:
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.');
});
最佳答案
我能够使用此插件将“content://”URI 转换为“file://”URI:https://www.npmjs.com/package/cordova-plugin-filepath .
获得“file://”URI 后,我就可以使用 Cordova 的 resolveLocalFileSystemURL() 函数。
希望这对您有所帮助。
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*/});
});
}
关于javascript - 手机间隙 : Resolving content://URI obtained from native file chooser,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28220118/