在Samsung文件资源管理器中打开自定义文件类型

问题描述 投票:5回答:1

我有两个自定义文件类型,我的应用程序可以处理,我希望能够从各种其他应用程序打开。我所拥有的功能包括Gmail和设置文件浏览器,但有几个第三个文件管理器(包括三星我的文件和Astro文件管理器)无法识别这些文件类型属于我的应用程序。有没有办法制作一个意图过滤器,使这些文件浏览器应用程序能够识别这些文件应该由我的应用程序打开?

这是现有的意图过滤器:

<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>
android
1个回答
1
投票

我现在有一个适用于包括三星在内的几个常见文件管理器的解决方案。您需要的是一个指定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>

编辑:

上面的解决方案将无法匹配b之前的路径中具有bloomd的文件。有关为何以及如何克服这些问题的详细信息,请参阅this answer

© www.soinside.com 2019 - 2024. All rights reserved.