Android 注册应用程序以打开自定义扩展文件也共享它

问题描述

我正在尝试让我的应用共享和处理自定义文件扩展名,点击它后,它会打开我的应用,然后我解析它。

我研究了不同的方法,例如 LIKE THISTHIS,但是例如从 Filebrowser 或 WhatsApp 中点击文件并没有检测到我的应用程序。

如果有帮助,我正在使用导航组件

我不确定我做错了什么,如果有人有一个可行的例子,我将不胜感激。

谢谢。

这是我试过的一些代码(我正在用 txt 进行测试,因为它是一个简单的扩展)(1)

 <intent-filter
                android:icon="@mipmap/ic_hp_launcher"
                android:label="@string/app_name"
                android:priority="999">
                <action android:name="android.intent.action.VIEW" />

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

                <data android:scheme="file" />
                <data android:host="*" />
                <data android:pathPattern="*.txt" />
            </intent-filter>

            <intent-filter
                android:icon="@mipmap/ic_hp_launcher"
                android:label="@string/app_name"
                android:priority="999">
                <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/txt" />
            </intent-filter>

我也试过(2)

 <intent-filter
                android:icon="@mipmap/ic_hp_launcher"
                android:label="@string/app_name"
                android:priority="999">
                <action android:name="android.intent.action.VIEW" />
                <category android:name="android.intent.category.DEFAULT" />

                <data android:scheme="file" />
                <data android:host="*" />
                <data android:pathPattern=".*\\.txt" />
            </intent-filter>

            <intent-filter
                android:icon="@mipmap/ic_hp_launcher"
                android:label="@string/app_name"
                android:priority="999">
                <action android:name="android.intent.action.VIEW" />
                <category android:name="android.intent.category.DEFAULT" />
                <data
                    android:host="*"
                    android:mimeType="*/*"
                    android:pathPattern=".*\\.txt"
                    android:scheme="file" />
            </intent-filter>

            <intent-filter
                android:icon="@mipmap/ic_hp_launcher"
                android:label="@string/app_name"
                android:priority="999">
                <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:mimeType="*/*" />
                <!--
                    Work around Android's ugly primitive PatternMatcher
                    implementation that can't cope with finding a . early in
                    the path unless it's explicitly matched.
                -->
                <data android:host="*" />
                <data android:pathPattern=".*\\.txt" />
                <data android:pathPattern=".*\\..*\\.txt" />
                <data android:pathPattern=".*\\..*\\..*\\.txt" />
                <data android:pathPattern=".*\\..*\\..*\\..*\\.txt" />
                <!-- keep going if you need more -->

            </intent-filter>

和 (3)

 <intent-filter
                android:icon="@mipmap/ic_hp_launcher"
                android:label="@string/app_name"
                android:priority="999">
                <action android:name="android.intent.action.VIEW" />

                <category android:name="android.intent.category.DEFAULT" />
                <data android:scheme="file" />
                <data android:mimeType="text/plain" />
            </intent-filter>

解决方法

我终于解决了我的问题,我正在同时测试多个东西。 我最终使用了

  <!-- this is needed for apps like whatsapp,it doesn't provide extension,so you can't add path  -->
            <intent-filter>
                <action android:name="android.intent.action.VIEW" />

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

                <data
                    android:mimeType="application/octet-stream"
                    android:scheme="content" />
            </intent-filter>

            <!-- this is needed for apps like explorer -->
            <intent-filter>
                <action android:name="android.intent.action.VIEW" />

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

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

如果您使用 mimeType="*/* 然后检查 logcat(根本没有过滤器),搜索 content:// 然后您会看到 Android 发送到您的应用程序的内容,然后修改 IntentFilter 以匹配您想要的内容。某些应用不发送扩展名,因此使用 pathPattern 无法使用它,还需要使用 schema = content

感谢@CommonsWare 的帮助。