美文网首页
Android 用自己的app打开自定义文件

Android 用自己的app打开自定义文件

作者: 霁逸lei | 来源:发表于2021-10-26 18:49 被阅读0次

    需求:发送文件到QQ微信,下载后点击其他方式打开选择自己的app

    • 问题1:如何把自己的app添加到系统列表(百度一波准备一步到位,结果啥也没出现)
    在AndroidManifest.xml中为activity添加相关配置
    <data android:scheme="file" android:host="*" android:mimeType="*/*" android:pathPattern=".*\\.stl"/>
    
    最后发现scheme="file" 改为添加scheme="http"正常了显示在列表
    <intent-filter android:scheme="http"
        tools:ignore="AppLinkUrlError">
        <action android:name="android.intent.action.VIEW"></action>
        <category android:name="android.intent.category.DEFAULT"></category>
        <data android:mimeType="*/*"></data>
    </intent-filter>
    
    uri 的组成如下: 
    
    <scheme>://<host>:<port>/[<path>|<pathPrefix>|<pathPattern>]
    scheme例如http、content、file。所以,data里面的scheme指的就是类似于协议的 东西,如果我们支持打开本地的文件,那就是file咯,别忘了android 7.0 以上以某种方式打开本地文件是content://xxx开头的,所以把content加上吧。详情参考下面的文章
    

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

    • 问题2筛选特定文件类型,设置pathPattern参数填写相关自定义后缀
    //pathPattern 中的意思你的路径样式,如第一个是在/storage/sdcard/emulated/0/下的文件能支持stl格式文件,第二个比第一个多了"/.*" 所以可以打开下一层的自定义文件
    <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:mimeType="*/*"></data>
        <data android:host="*" />
        <data android:scheme="file"/>
        <data android:scheme="content"/>
        <data android:scheme="http"/>
        <data android:scheme="https"/>
        <data android:pathPattern="/.*\\.tmp" />
        <data android:pathPattern="/.*\\..*\\.tmp" />
        <data android:pathPattern="/.*\\..*\\..*\\.tmp" />
        <data android:pathPattern="/.*\\..*\\..*\\..*\\.tmp" />
        <data android:pathPattern="/.*\\..*\\..*\\..*\\..*\\.tmp" />
        <data android:pathPattern="/.*\\..*\\..*\\..*\\..*\\..*\\.tmp" />
        <data android:pathPattern="/.*\\..*\\..*\\..*\\..*\\..*\\..*\\.tmp" />
        <data android:pathPattern="/.*\\..*\\..*\\..*\\..*\\..*\\..*\\..*\\.tmp" />
        <data android:pathPattern="/.*\\..*\\..*\\..*\\..*\\..*\\..*\\..*\\..*\\.tmp" />
        <data android:pathPattern="/.*\\..*\\..*\\..*\\..*\\..*\\..*\\..*\\..*\\..*\\.tmp" />
    </intent-filter>
    
    • 问题3 当app已启动跳转开启后intent.getData()参数为null
    先上结论onNewIntent调用setIntent(intent);
    
        @Override
        protected void onNewIntent(Intent intent) {
            Log.d("DrawerActivity", "onNewIntent:" + intent.getData());
            super.onNewIntent(intent);
            Log.d("DrawerActivity", "onNewIntent intent.getData():" + intent.getData());
        }
    
        @Override
        protected void onResume() {
            super.onResume();
            Log.d("DrawerActivity", "onResume intent.getData():" + getIntent().getData());
        }
    
    
    2021-10-26 16:39:08.916 1638-1638/com.example.myapplication D/DrawerActivity: onNewIntent:content://com.tencent.mobileqq.fileprovider/external_files/storage/emulated/0/Android/data/com.tencent.mobileqq/Tencent/QQfile_recv/test.txt
    2021-10-26 16:39:08.916 1638-1638/com.example.myapplication D/DrawerActivity: onNewIntent intent.getData():content://com.tencent.mobileqq.fileprovider/external_files/storage/emulated/0/Android/data/com.tencent.mobileqq/Tencent/QQfile_recv/test.txt
    2021-10-26 16:39:08.917 1638-1811/com.example.myapplication D/ZrHung.AppEyeUiProbe: restart watching
    2021-10-26 16:39:08.918 1638-1638/com.example.myapplication D/DrawerActivity: onResume intent.getData():null
    

    相关文章

      网友评论

          本文标题:Android 用自己的app打开自定义文件

          本文链接:https://www.haomeiwen.com/subject/qgbealtx.html