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

时间:2018-05-18 08:41:27

标签: android

我有两个自定义文件类型,我的应用程序可以处理,我希望能够从各种其他应用程序打开。我所拥有的功能包括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>

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

它表明,三星针对未知文件类型的“我的文件”根本不发送intent。因此,intent-filter不能在这里提供帮助。