Android关联文件类型,使得自己app支持打开【加入到“用其它应用打开”的列表中】

转自:https://blog.csdn.net/qq_36009027/article/details/94627156

 

有时候,我们开发的app需要支持打开某些文件,如何才能使得打开文件时系统能把我们开发的app列出来呢?

例如,

1.我开发了一个浏览器,那用户点击链接选择浏览器打开时,怎么关联上我们app可供选择

2.有时候我们app支持分享图片,那用户在系统相册选择分享时,怎么关联上我们app可供选择

3.最常见的一种情况,就是我们有时候数据文件的格式是自定义的,其他app不支持解析,但是从文件管理打开,怎么关联上我们app可供选择打开呢?

4....

上代码,再解释。

   <activity
            android:name=".main.MainActivity"
            android:screenOrientation="portrait" >
 
            <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="file"/>
                <data android:scheme="content"/>
                <data android:scheme="http"/>
                <data android:scheme="https"/>
                <data android:mimeType="*/*" />
            </intent-filter>
 
        </activity>
 

需要注意的是<intent-filter>里面的内容

首先第一个必不可少的就是

<action android:name="android.intent.action.VIEW" />
官方解释:

ACTION_VIEW
Added in API level 1
public static final String ACTION_VIEW
 
Activity Action: Display the data to the user. This is the most common action performed on data -- it is the generic action you can use on a piece of data to get the most reasonable thing to occur. For example, when used on a contacts entry it will view the entry; when used on a mailto: URI it will bring up a compose window filled with the information supplied by the URI; when used with a tel: URI it will invoke the dialer.
 
Input: getData() is URI from which to retrieve data.
 
Output: nothing.
 
Constant Value: "android.intent.action.VIEW"
简而言之,可以根据你的数据类型调用到你这个Activity来处理显示。

然后,第二个也是必不可少的是

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

官方解释:

CATEGORY_BROWSABLE
Added in API level 1
public static final String CATEGORY_BROWSABLE
 
Activities that can be safely invoked from a browser must support this category. For example, if the user is viewing a web page or an e-mail and clicks on a link in the text, the Intent generated execute that link will require the BROWSABLE category, so that only activities supporting this category will be considered as possible actions. By supporting this category, you are promising that there is nothing damaging (without user intervention) that can happen by invoking any matching Intent.
 
Constant Value: "android.intent.category.BROWSABLE"
翻译过来就是:可以从浏览器安全地调用的活动必须支持此类别。例如,如果用户正在查看网页或电子邮件并单击文本中的链接,则生成的Intent执行该链接将需要BROWSABLE类别,以便只有支持此类别的活动才会被视为可能的操作。通过支持此类别,您要承诺通过调用任何匹配的Intent来(进行操作)对用户来说无任何损害(无需用户干预)。

这个很重要的!假如你设置了action.VIEW,并且把<data>加上了,但是你忽略了category.BROWSABLE,那么恭喜你,你将会收获以下错误:

Activity supporting ACTION_VIEW is not set as BROWSABLE less... (Ctrl+F1) 
Inspection info:Ensure the URL is supported by your app, to get installs and traffic to your app from Google Search.  Issue id: AppLinkUrlError  More info: https://g.co/AppIndexing/AndroidStudio
看起来,似乎这样差不多了,然后把<data>也写对了,运行app,你会发现可选择列表中并没有你的app,为什么呢?

 

这还少了一个重要的东西,第三个必须要设置的东西

<category android:name="android.intent.category.DEFAULT" />
官方解释:

CATEGORY_DEFAULT
Added in API level 1
public static final String CATEGORY_DEFAULT
 
Set if the activity should be an option for the default action (center press) to perform on a piece of data. Setting this will hide from the user any activities without it set when performing an action on some data. Note that this is normally -not- set in the Intent when initiating an action -- it is for use in intent filters specified in packages.
 
Constant Value: "android.intent.category.DEFAULT"
设置Activity是否应该是作为一个段数据执行的选项的默认操作。设置此项后,将在用户对某些数据执行操作时,通过指定的intent过滤器匹配到数据,然后就可以启动你来这Activity来处理了。哦~原来这才是重中之重。

 

这里需要特别说明一下,当这个Activity设置了<category android:name="android.intent.category.LAUNCHER" />,你就不要加上<category android:name="android.intent.category.DEFAULT" />了,否则你会发现你的app没法打开了,因为安装了之后桌面根本没有图标!更不推荐你把处理这些数据(文件)的逻辑放在启动页。你应该把这个设置在一个不是启动Activity的Activity里面


 
 

最后一步,就是设置data了,指定你能支持的数据类型,没啥好说的,照写就好了

emm...还是讲一下吧,讲之前先了解一个东西:

uri 的组成如下: 

<scheme>://<host>:<port>/[<path>|<pathPrefix>|<pathPattern>]
scheme
例如http、content、file。
host
URI的主机名,比如www.jianshu.com。
port
端口号。
path/pathPattern/pathPrefix
path表示完整的路径。 pathPattern是判定完整路径是否匹配用的正则表达式。pathPrefix也是正则表达式,它匹配的是路径的前缀信息。
 

所以,data里面的scheme指的就是类似于协议的 东西,如果我们支持打开本地的文件,那就是file咯,别忘了android 7.0 以上以某种方式打开本地文件是content://xxx开头的,所以把content加上吧。

如果你需要支持网页打开,也不妨加上http和https。

奇怪?为什么我不设置host?不设置默认是“*”,也就是所有主机地址都支持,如果你需要特定过滤,你可以加上。

还有比较重要的一点是

mimeType
这个就是限定识别的文件类型,例如image/*,那么只有打开图片类型才能选择使用你这个app打开,如果你的app足够强大,支持所有格式,那么设置成 */* ,wow~好牛逼,什么格式都能打开。

当然,我也说了,有一些是自定义格式的,mineType不收集,我被迫设置成了 */* ,但是我又不想打开文件什么都出现我的app怎么办呢?看到uri构成的那个pathPattern没? 就是它!用它来过滤一下后缀,例如我的自定义后缀是 .abc和.def,那么我可以加上这两句就完美了

 <data android:pathPattern=".*\\.abc"/>
 <data android:pathPattern=".*\\.def"/>
好了,写到这里,编译运行,你已经可以愉快使用你的app打开这个类型的文件,美滋滋,再见,拜拜

 

--------------------------------------------------------------------------------------------------------------------------------------------------

别告诉我,选择你的app打开之后你不知道怎么获取文件信息

//在Activity的onCreate()或者onNewIntent()中
 
Intent intent = getIntent();
 
Uri uri = intent.getData();
 
if (uri != null) {
 
    String scheme= uri.getScheme();
 
    String host=uri.getHost();
 
    String port=uri.getPort()+"";
 
    String path=uri.getPath();
 
    String query=uri.getQuery();
 
}
//你把这些参数打印出来看看,你就知道怎么获取到那个文件或者网址了
 

我去喝枸杞了,拜~
--------------------- 
作者:天才小汪汪 
来源:CSDN 
原文:https://blog.csdn.net/qq_36009027/article/details/94627156 
版权声明:本文为博主原创文章,转载请附上博文链接!

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值