首页 > 解决方案 > 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>

标签: androidandroid-manifestintentfiltersharing

解决方案


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

  <!-- 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 以匹配您想要的内容。某些应用程序不发送扩展名,因此 usingpathPattern无法使用它,还schema = content需要using

感谢@CommonsWare 的帮助。


推荐阅读