Phonegap:解析content://从本机文件选择器获得的URI [英] Phonegap: Resolving content:// URI obtained from native file chooser

查看:152
本文介绍了Phonegap:解析content://从本机文件选择器获得的URI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Cordova Filechooser插件从我的Android设备中选择文件.插件返回一个content://URI(例如content://com.android.providers.media.documents/document/image%3A15756).我正在呼叫 resolveLocalFileSystemURI ,以便能够解析内容URL并在画布上绘制图像.但是由于某种原因,URI无法正确解析.

I am using the Cordova Filechooser plugin to select files from my Android device. The plugin returns a content:// URI (e.g. content://com.android.providers.media.documents/document/image%3A15756). I am making a call to resolveLocalFileSystemURI in order to be able to resolve the content URL and draw the image on a canvas. However for some reason the URI isn't being resolved properly.

例如返回的条目的fullPath为/com.android.providers.media.documents/document/image%3A15756 用于内容URI content://com.android.providers.media.documents/document/image%3A15756

E.g. The returned entry's fullPath is /com.android.providers.media.documents/document/image%3A15756 for the content URI content://com.android.providers.media.documents/document/image%3A15756

有什么想法吗?我的代码如下:

Any ideas? My code is as follows:

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:

I was able to convert from a "content://" URI to a "file://" URI using this plugin: https://www.npmjs.com/package/cordova-plugin-filepath.

获得"file://" URI后,我便可以使用Cordova的resolveLocalFileSystemURL()函数.

After obtaining the "file://" URI, I'm then able to use Cordova's resolveLocalFileSystemURL() function.

希望这会有所帮助.

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

这篇关于Phonegap:解析content://从本机文件选择器获得的URI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆