如何让我的应用程序无条件地打开文件和网址扩展名?
我在设置我的intent-filter
时已经结束了,因为它没有任何意义。我的最终目标是打开在某个扩展名中以path
结尾的任何内容。举个例子,让我们选择“。riley”作为我的目标延伸。
<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>
目前可以从file
和content
打开。
对于初学者来说,好像data
参数pathPattern
没有做任何事情,正如我所设定的那样:
<data
android:host="*"
android:mimeType="*/*"
android:pathPattern=".*\\.riley" />
..但我仍然可以打开Intent
Uri
个file
路径,这些路径不会以content
和{{1}下的“。riley”结尾方案。
我的应用程序使用此intent-filter
打开的几个示例路径是:
Path: /storage/sdcard0/Download/MyApp.apk
Path: /messagesByAccount/1
为了清晰起见,“路径:”只是我在日志中添加的内容。第一个来自file
方案下的gmail,第二个来自content
方案下的gtalk。鉴于此,我已经对intent-filters
如何工作感到困惑。
pathPattern
和file
计划忽略了content
。
我试图让我的应用程序从浏览器打开“。riley”扩展程序,但重定向链接没有MIME类型,因此我删除了mimeType
值来自data
模式(之前我有android:mimeType="*/*"
,因为MIME类型不一致)并且允许我从重定向URL打开就好了。
当我尝试从浏览器打开直接的“。riley” URL(例如:http://thisisnotarealdomainname.com/myfile.riley)并且从我的应用程序打开的选项消失时,出现了问题。将mimeType
值添加回data
模式允许我从直接网址开启。
intent-filter
不允许我打开直接和间接网址。
我希望能够从多种不同的方案类型中打开(例如:http,
文件,
内容), but as soon as I remove
方案altogether, it disqualifies my application from opening the
http scheme and when I specify
scheme =“*”, it disqualifies my application from opening ANY scheme. I've also tried adding more
数据模式,但我感觉好像只有一个生效,因为这对我没有任何帮助。
因此,指定http
将允许http
,但显然不允许file
或content
并且未指定scheme
禁用{{} 1}}。
我无法从所有不同的方案中打开。
重申一下, 如果给出这些复杂性,我怎样才能让我的应用程序无条件地打开文件和网址扩展 ?
答案 0 :(得分:3)
问题#1:<data android:pathPattern=".*\\.riley" />
无效。引用the documentation:
仅当为过滤器指定了方案和主机属性时,[pathPattern]才有意义。
问题#2:&#34;但重定向链接不具有MIME类型&#34;应该是不正确的,除非Web服务器严重破坏。 HTTP重定向本身没有MIME类型,但应该由浏览器本身处理,因为在浏览器处理重定向之前,它不知道URL是否指向它应该处理的东西(例如,网页)或者应该下载的东西。重定向的结果 - HTTP 200 OK响应 - 应该具有MIME类型。如果您确信重定向到下载的URL不起作用,请创建一个演示错误的示例项目并将其发布到某处以供审阅。
问题#3:&#34;我不能从所有不同的方案开放。&#34; - 在单独的<data>
元素中使用多个android:scheme
元素和多个<intent-filter>
属性。
例如,AOSP音乐应用程序具有:
<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>
您会注意到所有三个<intent-filter>
节中的操作,类别和MIME类型都相同;只有android:scheme
属性不同。
现在,此音乐应用示例的file
方案仅在某些本地文件管理器将适当的MIME类型附加到Intent
时才有效。音乐不会尝试使用android:pathPattern
来匹配文件扩展名。事实上,Google很少使用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上有很多关于其使用的问题。