android 选择文件的意图过滤器在华为 P8 和 LG F70 手机上不起作用

android intent filter for pick file not working on Huawei P8 and LG F70 phones

我想用我的应用程序打开 *.gpx 文件。在一些资源之后,我将这些意图过滤器放入我的清单文件中:

<!-- Intent-filter for Intents that contain the file suffix. -->
<intent-filter>
    <action android:name="android.intent.action.VIEW"/>

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

    <!--  For a path to be meaningful, both a scheme and an authority must be specified. -->
    <data
        android:host="*"
        android:mimeType="*/*"
        android:pathPattern=".*\.gpx"
        android:scheme="file"/>
</intent-filter>

<!-- Intent-filter for Intents that contain a MIME type -->
<intent-filter>
    <action android:name="android.intent.action.VIEW"/>

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

    <!-- This is the original mimeType which was used when creating the file. -->
    <data android:mimeType="application/gpx+xml"/>

    <!-- Some apps (e.g. some versions of Gmail) use the file suffix as the mimeType! -->
    <data android:mimeType="application/gpx"/>
</intent-filter>

<!-- Gmail sometimes uses some strange mimeTypes when opening attachments -->
<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="content"
        android:host="gmail-ls"
        android:mimeType="application/octet-stream"/>
</intent-filter>

它适用于大多数 android 版本和手机。

但据我所知,在 android 版本 4.4.2 的 LG F70 和使用默认文件浏览器的 android 版本 5.0.1 的华为 P8 Lite 上,它无法正常工作。看图片:

华为P8青春版:

LG F70:

有没有人以前遇到过这种情况?你知道我需要哪个意图过滤器吗? 任何答案都会非常感激!

您的第二个 <intent-filter> 在所有设备上都应该没有意义,因为没有 android:scheme 就不可能有 android:host

或者:

  • 删除 android:host 属性,或

  • 将那个特定的 <data> 元素移动到它自己的 <intent-filter> 中,并带有相关 actions/categories 的副本,并带有 android:scheme content

两者之间的区别在于您是想尝试处理来自任何来源(第一个项目符号)还是仅来自 Gmail 的内容提供商(第二个项目符号)的 application/octet-stream

您的初始代码在正确的轨道上。以下是如何让它发挥作用:

        <!-- Handle opening attachments with file explorer -->
        <intent-filter android:priority="100">
            <action android:name="android.intent.action.VIEW"/>
            <category android:name="android.intent.category.DEFAULT"/>
            <category android:name="android.intent.category.BROWSABLE"/>
            <data android:host="*" android:mimeType="application/xml"
                  android:pathPattern=".*\.gpx"
                  android:scheme="file"/>
        </intent-filter>
        <intent-filter android:priority="100">
            <action android:name="android.intent.action.VIEW"/>
            <category android:name="android.intent.category.DEFAULT"/>
            <category android:name="android.intent.category.BROWSABLE"/>
            <data android:host="*"
                  android:mimeType="*/*"
                  android:pathPattern=".*\.gpx"
                  android:scheme="content"/>
        </intent-filter>

        <!-- Handle opening attachments with Downloads app -->
        <intent-filter android:priority="100">
            <action android:name="android.intent.action.VIEW"/>
            <action android:name="android.intent.action.GET_CONTENT"/>
            <action android:name="android.intent.action.OPEN_DOCUMENT"/>
            <action android:name="android.intent.action.PICK"/>
            <category android:name="android.intent.category.OPENABLE"/>
            <category android:name="android.intent.category.DEFAULT"/>
            <category android:name="android.intent.category.BROWSABLE"/>
            <data android:mimeType="application/xml" android:pathPattern=".*\.gpx"
                  android:scheme="content"/>
            <data android:mimeType="application/octet-stream" android:pathPattern=".*\.gpx"
                  android:scheme="content"/>
        </intent-filter>
        <intent-filter android:priority="100">
            <action android:name="android.intent.action.VIEW"/>
            <action android:name="android.intent.action.GET_CONTENT"/>
            <action android:name="android.intent.action.OPEN_DOCUMENT"/>
            <action android:name="android.intent.action.PICK"/>
            <category android:name="android.intent.category.OPENABLE"/>
            <category android:name="android.intent.category.DEFAULT"/>
            <category android:name="android.intent.category.BROWSABLE"/>
            <data android:mimeType="application/xml" android:pathPattern=".*\.gpx"
                  android:scheme="file"/>
            <data android:mimeType="application/octet-stream" android:pathPattern=".*\.gpx"
                  android:scheme="file"/>
        </intent-filter>

        <!-- Handle opening attachments with Gmail -->
        <intent-filter android:priority="100">
            <action android:name="android.intent.action.VIEW"/>
            <action android:name="android.intent.action.GET_CONTENT"/>
            <data android:host="gmail-ls" android:mimeType="application/octet-stream"
                  android:scheme="file"/>
            <category android:name="android.intent.category.BROWSABLE"/>
            <category android:name="android.intent.category.DEFAULT"/>
        </intent-filter>
        <intent-filter android:priority="100">
            <action android:name="android.intent.action.VIEW"/>
            <action android:name="android.intent.action.GET_CONTENT"/>
            <data android:host="gmail-ls" android:mimeType="application/octet-stream"
                  android:scheme="content"/>
            <category android:name="android.intent.category.BROWSABLE"/>
            <category android:name="android.intent.category.DEFAULT"/>
        </intent-filter>
        <intent-filter android:priority="100">
            <action android:name="android.intent.action.VIEW"/>
            <action android:name="android.intent.action.GET_CONTENT"/>
            <data android:host="gmail-ls" android:mimeType="application/xml"
                  android:scheme="file"/>
            <category android:name="android.intent.category.BROWSABLE"/>
            <category android:name="android.intent.category.DEFAULT"/>
        </intent-filter>
        <intent-filter android:priority="100">
            <action android:name="android.intent.action.VIEW"/>
            <action android:name="android.intent.action.GET_CONTENT"/>
            <data android:host="gmail-ls" android:mimeType="application/xml"
                  android:scheme="content"/>
            <category android:name="android.intent.category.BROWSABLE"/>
            <category android:name="android.intent.category.DEFAULT"/>
        </intent-filter>

这里的关键是处理正确的 MIME 类型和类别,并假定在设备上打开文件的各种方式(即文件浏览器、下载应用程序、gmail 等)。