PhoneGap的resolveLocalFileSystemURL不适合在Android上的内容URI工作 [英] phonegap resolveLocalFileSystemURL does not work for content uri on android

查看:3095
本文介绍了PhoneGap的resolveLocalFileSystemURL不适合在Android上的内容URI工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用的Nexus 4手机上的Andr​​oid版本5.1.1接收来自Android的图像库共享图像。我使用的PhoneGap 4.2和PhoneGap的WebIntent插件 GitHub的链接和PhoneGap的插件文件的文档链接

文本和链接工作正常,但是当我试图从Android图库分享图片,我得到一个URI是这样的:


  

content://com.google.android.apps.photos.contentprovider/0/1/content%3A%2F%2Fmedia%2Fexternal%2Fimages%2Fmedia%2F63131/ACTUAL


而不是像文件:/// ....

我试图用window.resolveLocalFileSystemURL功能来解决这个URL,它返回成功的一个FileEntry的对象。然而,当我尝试使用它来读取文件,我得到code 1000错误(见下文code和输出)。

有趣的是,如果显示器通过img标签这个网址,我能看到图像细腻。

 < IMG SRC =ID =test_intent_image>
$('#test_intent_image')ATTR(src用户,URI)。

这说明问题可能是window.resolveLocalFileSystemURL功能,不能处理的内容:// ...类型的URI是否正确?

=====参考信息======

下面是code我使用(注意:我想这与其他文件操作,如将copyTo()到app文件夹,但它给了同样的错误):

  document.addEventListener(deviceready,共享,假的);
    document.addEventListener(恢复,共享,假的);
 共享功能(){window.plugins.webintent.getExtra(window.plugins.webintent.EXTRA_STREAM,
功能(数据){
//数据是EXTRA_TEXT的值
    如果(!数据){
        返回;
    }    window.resolveLocalFileSystemURL(
        数据,
        功能(进入){
            的console.log('文件条目:+ JSON.stringify(条目,空,3));            功能赢(文件){
                VAR读卡器=新的FileReader();
                reader.onloadend =功能(EVT){
                    的console.log(文件读取成功);
                    的console.log(evt.target.result);
                };                reader.readAsText(文件);
            };            功能失效(错误){
                的console.log(文件读取错误:+错误code);
            };            entry.file(赢,失败);        },
        功能(错误){
            的console.log('文件错误:'+误差);
        });},函数(){
    //有没有额外的供应。
    // debug_log(网络意图getExtra:没有多余的);
    }
);

}

下面是code的控制台输出,当我试图从共享图片库的图像:

 手柄股流:\"content://com.google.android.apps.photos.contentprovider/0/1/content%3A%2F%2Fmedia%2Fexternal%2Fimages%2Fmedia%2F63087/ACTUAL\"
文件条目:{
   ISFILE:真实,
   isDirectory:假的,
   名:实际,
   完整路径: \"/com.google.android.apps.photos.contentprovider/0/1/content%3A//media/external/images/media/63087/ACTUAL\",
   文件系统:<文件系统:内容>中,
   nativeURL: \"content://com.google.android.apps.photos.contentprovider/0/1/content%3A%2F%2Fmedia%2Fexternal%2Fimages%2Fmedia%2F63087/ACTUAL\"
}
文件读取错误:1000

下面是在AndroidManifest.xml文件的意图过滤器。注:共享工作得很好,只是没有能够解决URL所以这可能不是一个问题。

 <意向滤光器>
<作用机器人:名字=android.intent.action.SEND/>
            <类机器人:名字=android.intent.category.DEFAULT/>
            <数据机器人:mime类型=* / */>
        &所述; /意图滤光器>


解决方案

我一直有完全相同的问题,使用标准的文件的插件并没有直接的解决办法。

我想这个插件但是 https://github.com/hiddentao/cordova-plugin-filepath 这仅适用于Android内容的URI。

您使用此功能,而是和它似乎是工作。

window.FilePath.resolveNativePath('内容:// ......,successCallback,errorCallback);

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>

解决方案

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);

这篇关于PhoneGap的resolveLocalFileSystemURL不适合在Android上的内容URI工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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