Phonegap:解决从本地文件选择器获取的content:// URI问题

4
我正在使用Cordova Filechooser插件从我的Android设备中选择文件。该插件返回一个内容:// URI(例如content://com.android.providers.media.documents/document/image%3A15756)。我调用resolveLocalFileSystemURI,以便能够解析内容URL并在画布上绘制图像。然而,由于某种原因,URI无法正确解析。
例如,返回条目的fullPath是/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.');
        });

1
不一定有一个可以访问的文件。该插件虽然名为“文件选择器”,但实际上并没有实现它。它使用Android的ACTION_GET_CONTENT实现了内容选择器。返回的是一个Uri,本地Java应用程序将使用它来获取InputStream以读取所选内容。并不要求Uri是您可以访问的文件,随着每个后续的Android操作系统版本,其作为您可以访问的文件的可能性会逐渐减小。 - CommonsWare
我明白了。所访问的文件来自画廊,该画廊可由我的应用程序访问...您有任何其他推荐的插件吗? - JB2
抱歉,我没有给你任何建议。 - CommonsWare
已解决:https://dev59.com/xoTca4cB1Zd3GeqPAN6X - JB2
1个回答

4
我能够使用这个插件:https://www.npmjs.com/package/cordova-plugin-filepath,将“content://”URI转换为“file://”URI。

获取“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*/});
    });
}

我已经尝试过并且它可以工作,但是如果我尝试复制或读取文件,我会收到一个SECURITY_ERR错误。我想从Android上的Outlook获取文件的内容。 - hydrococcus

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接