在Samsung File Explorer中打开自定义文件类型

Ric*_*rad 11 android

我有两种自定义文件类型,我的应用程序可以处理这些文件类型,我希望能够从各种其他应用程序中打开它们。我为许多人(包括Gmail和“设置”文件浏览器)工作的东西,但是还有一些第三种文件管理器(包括Samsung My Files和Astro File Manager)无法识别这些文件类型属于我的应用程序。有没有一种方法可以使这些文件浏览器应用程序识别出应该由我的应用程序打开这些文件的意图过滤器?

这是现有的意图过滤器:

<intent-filter>
    <action android:name="android.intent.action.MAIN" />

    <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>

<!-- See http://stackoverflow.com/questions/1733195/android-intent-filter-for-a-particular-file-extension/2062112#2062112 -->


<!--
        Capture content by MIME type, which is how Gmail broadcasts
        attachment open requests.  pathPattern and file extensions
        are ignored, so the MIME type *MUST* be explicit, otherwise
        we will match absolutely every file opened.
-->
<intent-filter
    android:icon="@drawable/book"
    android:label="@string/app_name"
    android:priority="50">
    <action android:name="android.intent.action.VIEW" />

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

    <!-- needed for properly formatted email messages -->
    <data
        android:mimeType="application/vnd.bloom"
        android:scheme="content" />
    <!-- needed for mangled email messages -->
    <data
        android:mimeType="application/bloom"
        android:scheme="content" />
    <!-- needed for properly formatted email messages -->
    <data
        android:mimeType="application/vnd.bloomd"
        android:scheme="content" />
    <!-- needed for mangled email messages -->
    <data
        android:mimeType="application/bloomd"
        android:scheme="content" />
    <!-- needed for properly formatted email messages -->
    <data
        android:mimeType="application/vnd.bloombundle"
        android:scheme="content" />
    <!-- needed for mangled email messages -->
    <data
        android:mimeType="application/bloombundle"
        android:scheme="content" />
    <!-- needed for mangled email messages -->
    <data
        android:mimeType="application/octet-stream"
        android:scheme="content" />
</intent-filter>

<!--
        Capture file open requests (pathPattern is honoured) where no
        MIME type is provided in the Intent.  An Intent with a null
        MIME type will never be matched by a filter with a set MIME
        type, so we need a second intent-filter if we wish to also
        match files with this extension and a non-null MIME type
        (even if it is non-null but zero length).
-->
<intent-filter
    android:icon="@drawable/book"
    android:label="@string/app_name"
    android:priority="50">
    <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="*" />

    <!--
            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:pathPattern=".*\\.bloomd" />
    <data android:pathPattern=".*\\..*\\.bloomd" />
    <data android:pathPattern=".*\\..*\\..*\\.bloomd" />
    <data android:pathPattern=".*\\..*\\..*\\..*\\.bloomd" />
    <data android:pathPattern=".*\\..*\\..*\\..*\\..*\\.bloomd" />
    <data android:pathPattern=".*\\..*\\..*\\..*\\..*\\..*\\.bloomd" />
    <data android:pathPattern=".*\\..*\\..*\\..*\\..*\\..*\\..*\\.bloomd" />
    <data android:pathPattern=".*\\.bloombundle" />
    <data android:pathPattern=".*\\..*\\.bloombundle" />
    <data android:pathPattern=".*\\..*\\..*\\.bloombundle" />
    <data android:pathPattern=".*\\..*\\..*\\..*\\.bloombundle" />
    <data android:pathPattern=".*\\..*\\..*\\..*\\..*\\.bloombundle" />
    <data android:pathPattern=".*\\..*\\..*\\..*\\..*\\..*\\.bloombundle" />
    <data android:pathPattern=".*\\..*\\..*\\..*\\..*\\..*\\..*\\.bloombundle" />
</intent-filter>

<!--
        Capture file open requests (pathPattern is honoured) where a
        (possibly blank) MIME type is provided in the Intent.  This
        filter may only be necessary for supporting ES File Explorer,
        which has the probably buggy behaviour of using an Intent
        with a MIME type that is set but zero-length.  It's
        impossible to match such a type except by using a global
        wildcard.
-->
<intent-filter
    android:icon="@drawable/book"
    android:label="@string/app_name"
    android:priority="50">
    <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: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:pathPattern=".*\\.bloomd" />
    <data android:pathPattern=".*\\..*\\.bloomd" />
    <data android:pathPattern=".*\\..*\\..*\\.bloomd" />
    <data android:pathPattern=".*\\..*\\..*\\..*\\.bloomd" />
    <data android:pathPattern=".*\\..*\\..*\\..*\\..*\\.bloomd" />
    <data android:pathPattern=".*\\..*\\..*\\..*\\..*\\..*\\.bloomd" />
    <data android:pathPattern=".*\\..*\\..*\\..*\\..*\\..*\\..*\\.bloomd" />
    <data android:pathPattern=".*\\.bloombundle" />
    <data android:pathPattern=".*\\..*\\.bloombundle" />
    <data android:pathPattern=".*\\..*\\..*\\.bloombundle" />
    <data android:pathPattern=".*\\..*\\..*\\..*\\.bloombundle" />
    <data android:pathPattern=".*\\..*\\..*\\..*\\..*\\.bloombundle" />
    <data android:pathPattern=".*\\..*\\..*\\..*\\..*\\..*\\.bloombundle" />
    <data android:pathPattern=".*\\..*\\..*\\..*\\..*\\..*\\..*\\.bloombundle" />
</intent-filter>
Run Code Online (Sandbox Code Playgroud)

Ric*_*rad 2

我现在有了一个适用于包括三星在内的多种常见文件管理器的解决方案。您需要的是一个指定 MIME 类型的过滤器,以及一个不指定 MIME 类型并根据文件扩展名进行过滤的单独过滤器。我又添加了一个使用通配符 MIME 类型和基于文件扩展名的过滤器,因为至少有一个文件管理器似乎指定了空 MIME 类型。

关于元素在过滤器中到底如何工作的讨论值得仔细阅读多次:https://developer.android.com/guide/components/intents-filters#DataTest

解决方案:

    <!-- Matches intents by MIME type -->
    <intent-filter
        android:icon="@drawable/book"
        android:label="@string/app_name">
        <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" />
        <data android:scheme="file" />
        <data android:mimeType="application/vnd.bloom" />
        <data android:mimeType="application/bloom" />
        <data android:mimeType="application/vnd.bloomd" />
        <data android:mimeType="application/bloomd" />
        <data android:mimeType="application/vnd.bloombundle" />
        <data android:mimeType="application/bloombundle" />
        <data android:mimeType="application/octet-stream" />
    </intent-filter>

    <!-- Matches intents by file extension -->
    <intent-filter
        android:icon="@drawable/book"
        android:label="@string/app_name">
        <action android:name="android.intent.action.VIEW" />
        <action android:name="android.intent.action.SEND" />

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

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

    <!-- Matches intents by file extension when an empty MIME type is set -->
    <intent-filter
        android:icon="@drawable/book"
        android:label="@string/app_name">
        <action android:name="android.intent.action.VIEW" />
        <action android:name="android.intent.action.SEND" />

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

        <data android:mimeType="*/*" />

        <data android:scheme="content" />
        <data android:scheme="file" />
        <data android:host="*" />
        <data android:pathPattern=".*bloomd" />
        <data android:pathPattern=".*bloombundle" />
    </intent-filter>
Run Code Online (Sandbox Code Playgroud)

编辑:

b上面的解决方案将无法匹配路径中包含 之前的文件bloomd。有关原因以及如何克服该问题的详细信息,请参阅此答案。

  • 不幸的是,这在运行 Android 8 的三星 S8 上不起作用。尝试使用“我的文件”应用程序打开自定义文件类型会创建一个没有 mimetype 和 Uri 的意图,如“content://0@media/external/file/123” 。因此,pathPattern 在这里不起作用...我发现的唯一解决方案是通过删除pathPattern 来注册所有文件,这似乎是一个糟糕的解决方案。 (4认同)