首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Android意图过滤器:将app与文件扩展名关联

Android意图过滤器:将app与文件扩展名关联
EN

Stack Overflow用户
提问于 2010-09-21 20:32:05
回答 14查看 77.6K关注 0票数 96

我有一个自定义文件类型/扩展名,我想要将我的应用程序与其关联。

据我所知,数据元素就是为了这个目的而创建的,但是我不能让它工作。http://developer.android.com/guide/topics/manifest/data-element.html根据文档和许多论坛的帖子,它应该是这样工作的:

代码语言:javascript
复制
<intent-filter>
    <action android:name="android.intent.action.MAIN" />
    <category android:name="android.intent.category.DEFAULT" />
    <category android:name="android.intent.category.BROWSABLE" />
    <data android:mimeType="application/pdf" />
</intent-filter>

好吧,它不起作用。我做错什么了?我只想声明我自己的文件类型。

EN

回答 14

Stack Overflow用户

回答已采纳

发布于 2011-02-24 06:00:35

您需要多个意图过滤器来处理您想要处理的不同情况。

示例1,处理不带mimetype的http请求:

代码语言:javascript
复制
  <intent-filter>
    <action android:name="android.intent.action.VIEW" />
    <category android:name="android.intent.category.BROWSABLE" />
    <category android:name="android.intent.category.DEFAULT" />
    <data android:scheme="http" />
    <data android:host="*" />
    <data android:pathPattern=".*\\.pdf" />
  </intent-filter>

处理mimetype,其中后缀是不相关的:

代码语言:javascript
复制
  <intent-filter>
    <action android:name="android.intent.action.VIEW" />
    <category android:name="android.intent.category.BROWSABLE" />
    <category android:name="android.intent.category.DEFAULT" />
    <data android:scheme="http" />
    <data android:host="*" />
    <data android:mimeType="application/pdf" />
  </intent-filter>

处理来自文件浏览器应用程序的意图:

代码语言:javascript
复制
  <intent-filter>
    <action android:name="android.intent.action.VIEW" />
    <category android:name="android.intent.category.DEFAULT" />
    <data android:scheme="file" />
    <data android:host="*" />
    <data android:pathPattern=".*\\.pdf" />
  </intent-filter>
票数 117
EN

Stack Overflow用户

发布于 2012-10-16 20:59:42

其他的解决方案对我来说并不可靠,直到我添加了:

代码语言:javascript
复制
android:mimeType="*/*" 

在此之前,它在一些应用程序中工作,在一些应用程序中不起作用。

为我提供完整的解决方案:

代码语言:javascript
复制
<intent-filter>
  <action android:name="android.intent.action.VIEW" />
  <category android:name="android.intent.category.DEFAULT" />
  <data android:scheme="file"  android:host="*" android:pathPattern=".*\\.EXT" android:mimeType="*/*"  />
</intent-filter>
票数 50
EN

Stack Overflow用户

发布于 2012-08-28 13:44:18

The pathPattern

代码语言:javascript
复制
<data android:pathPattern=".*\\.pdf" />

如果文件路径在".pdf“前包含一个或多个点,则不起作用。

这将会起作用:

代码语言:javascript
复制
<data android:pathPattern=".*\\.pdf" />
<data android:pathPattern=".*\\..*\\.pdf" />
<data android:pathPattern=".*\\..*\\..*\\.pdf" />
<data android:pathPattern=".*\\..*\\..*\\..*\\.pdf" />

如果你想支持更多的点,可以添加更多的点。

票数 17
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3760276

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档