【问题标题】:Open my application for a particular file and URL extension - intent-filter isn't working as intended打开我的应用程序以获取特定文件和 URL 扩展名 - 意图过滤器未按预期工作
【发布时间】:2013-03-15 14:12:58
【问题描述】:

问题:

如何让我的应用程序无条件地打开文件和 url 扩展名?

设置我的intent-filter 我已经束手无策了,因为这没有任何意义。我的最终目标是打开任何以某个扩展名结尾的path。举个例子,让我们选择 ".riley" 作为我的目标扩展。

我的基础`intent-filter`

<intent-filter>
    <action android:name="android.intent.action.VIEW" />

    <category android:name="android.intent.category.DEFAULT" />
    <category android:name="android.intent.category.BROWSABLE" />

    <data
        android:host="*"
        android:mimeType="*/*"
        android:pathPattern=".*\\.riley" />
</intent-filter>

目前可以从filecontent 打开。

问题 #1:路径模式

对于初学者来说,data 参数 pathPattern 似乎并没有像我设置的那样做任何事情:

<data
    android:host="*"
    android:mimeType="*/*"
    android:pathPattern=".*\\.riley" />

.. 但我仍然可以在 filecontent 方案下使用不以 ".riley" 结尾的 Uri 路径打开 Intents。

我的应用程序使用此 intent-filter 打开的几个示例路径是:

Path: /storage/sdcard0/Download/MyApp.apk Path: /messagesByAccount/1

为了清楚起见,“路径:”只是我自己在日志中添加的。第一个来自file 方案下的gmail,第二个来自content 方案下的gtalk。鉴于此,我已经对 intent-filters 的工作方式感到困惑。

pathPatternfilecontent 方案中被忽略。

问题 #2:在浏览器中打开

我试图让我的应用程序从浏览器打开 ".riley" 扩展,但是重定向链接没有 MIME 类型,所以我从data 模式(我之前有 android:mimeType="*/*",因为 MIME 类型不一致),这让我可以很好地从重定向 URL 中打开。

当我尝试从浏览器(例如:http://thisisnotarealdomainname.com/myfile.riley)打开直接 ".riley" URL 并且从我的应用程序打开的选项消失时,问题出现了。将mimeType 值添加回我的data 模式允许我从直接URL 打开。

intent-filter 不允许我打开直接和间接 URL。

问题 #3:方案

我希望能够从多种不同的方案类型中打开(例如:http,file,content), but as soon as I removeschemealtogether, it disqualifies my application from opening thehttpscheme and when I specifyscheme="*", it disqualifies my application from opening ANY scheme. I've also tried adding moredata`模式,但我感觉好像只有一个生效,因为这对我一点帮助都没有。

因此,指定http 将允许http,但显然不允许filecontent,并且不指定scheme 将禁用http

我无法从所有不同的方案中打开。

重申一下,考虑到这些并发症,我如何才能让我的应用程序无条件地打开文件和 url 扩展名

【问题讨论】:

    标签: android android-intent mime-types intentfilter


    【解决方案1】:

    问题 #1:&lt;data android:pathPattern=".*\\.riley" /&gt; 完全无效。引用the documentation:

    [pathPattern] 仅当还为过滤器指定了方案和主机属性时才有意义。

    问题 #2:“但是重定向链接没有 MIME 类型”应该是不正确的,除非 Web 服务器严重损坏。 HTTP 重定向本身没有 MIME 类型,但应该由浏览器自己处理,因为在浏览器处理重定向之前,它不知道 URL 是否指向它应该处理的东西(例如,网页)或者应该下载的东西。重定向的结果——HTTP 200 OK 响应——应该有一个 MIME 类型。如果您确信重定向到下载的 URL 不起作用,请创建一个示例项目来展示错误并将其发布到某处以供审核。

    问题 #3:“我无法从所有不同的方案中打开。” -- 在单独的&lt;intent-filter&gt; 元素中使用具有多个android:scheme 属性的多个&lt;data&gt; 元素。

    例如,AOSP Music 应用有:

        <activity android:name="AudioPreview" android:theme="@android:style/Theme.Dialog"
                android:taskAffinity=""
                android:excludeFromRecents="true" android:exported="true" >
            <intent-filter>
                <action android:name="android.intent.action.VIEW" />
                <category android:name="android.intent.category.DEFAULT" />
                <data android:scheme="file"/>
                <data android:mimeType="audio/*"/>
                <data android:mimeType="application/ogg"/>
                <data android:mimeType="application/x-ogg"/>
                <data android:mimeType="application/itunes"/>
            </intent-filter>
            <intent-filter>
                <action android:name="android.intent.action.VIEW" />
                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />
                <data android:scheme="http" />
                <data android:mimeType="audio/*"/>
                <data android:mimeType="application/ogg"/>
                <data android:mimeType="application/x-ogg"/>
                <data android:mimeType="application/itunes"/>
            </intent-filter>
            <intent-filter
                android:priority="-1">
                <action android:name="android.intent.action.VIEW" />
                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />
                <data android:scheme="content" />
                <data android:mimeType="audio/*"/>
                <data android:mimeType="application/ogg"/>
                <data android:mimeType="application/x-ogg"/>
                <data android:mimeType="application/itunes"/>
            </intent-filter>
        </activity>
    

    您会注意到所有三个&lt;intent-filter&gt; 节中的动作、类别和MIME 类型都是相同的;只有android:scheme 属性不同。

    现在,此音乐应用示例的 file 方案仅在某些本地文件管理器将适当的 MIME 类型附加到 Intent 时才有效;音乐不会尝试使用android:pathPattern 来匹配文件扩展名。事实上,谷歌很少使用android:pathPattern。在 AOSP 日历应用中出现了一次:

            <intent-filter
               android:priority="50">
               <action android:name="android.intent.action.VIEW" />
               <category android:name="android.intent.category.DEFAULT" />
               <category android:name="android.intent.category.BROWSABLE" />
               <data android:scheme="http" android:host="www.google.com" android:pathPrefix="/calendar/event" />
               <data android:scheme="https" android:host="www.google.com" android:pathPrefix="/calendar/event" />
               <data android:scheme="http" android:host="www.google.com" android:pathPattern="/calendar/hosted/.*/event" />
               <data android:scheme="https" android:host="www.google.com" android:pathPattern="/calendar/hosted/.*/event" />
            </intent-filter>
    

    但仅此而已。当然其他人已经尝试过了——StackOveflow 上有很多关于它的使用的问题。

    【讨论】:

    • 我希望我能在 cmets 中回复得足够干净!对于问题 #1,我将在我的问题的编辑中澄清,当我说“以及其他参数”时,我的意思是 data 标记声明了 schemehost,与它们的声明方式相同在上面的例子中。同样,我将对其进行编辑。对于问题 #2,我知道不返回 MIME 类型的 Web 脚本的一个示例是当网页使用 Javascript 的window.location 重定向时。我可以指定 &lt;data android:scheme="http"/&gt; intent-filter 来捕获它并验证 MIME 类型是 null
    • 至于问题 #3,您所说的效果非常好,而且它比文档对我所做的更有意义。非常感谢!
    • 另外,我从&lt;data/&gt; sn-p 中省略了schemehost 的原因是希望清理问题以仅显示相关信息并使其更容易然而,通读一遍,它似乎产生了相反的效果。
    • @RileyE: "redirects with Javascript's window.location" -- 是的,我并不惊讶这给你带来了困难,因为这不是 HTTP 重定向。关于问题 #1,尝试找到一些使用 pathPattern 的开源 Android 应用(Google 代码搜索,我好想你!),看看它是如何处理的。
    • 谷歌代码搜索可能没了,但至少还有http://symbolhound.com/!但我将不得不承受window.location 的损失。现在其他一切都按预期工作。所以,谢谢!
    猜你喜欢
    • 1970-01-01
    • 2010-12-16
    • 1970-01-01
    • 1970-01-01
    • 2018-06-30
    • 2013-03-04
    • 2013-10-03
    • 1970-01-01
    • 2011-04-15
    相关资源
    最近更新 更多