Home » Android » android – Open custom filetype in Samsung file explorer

android – Open custom filetype in Samsung file explorer

Posted by: admin May 14, 2020 Leave a comment

Questions:

I have two custom filetypes that my app can handle that I would like to be able to open from a wide variety of other apps. What I have is working for many including Gmail and the Settings file explorer, but there several third file managers (including Samsung My Files and Astro File Manager) that do not recognize these filetypes as belonging to my app. Is there a way to craft an intent filter that will enable these file explorer apps to recognize that these files should be opened by my app?

Here’s the existing intent filters:

<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>
How to&Answers:

I now have a solution that works for several common file managers including Samsung’s. What you need is one filter that specifies a MIME type, and a separate filter that does not specify a MIME type and filters based on file extension. I added one more that uses a wildcard MIME type and filters based on file extension since at least one file manager seemed to specifying an empty MIME type.

The discussion of exactly how elements work in the filter is worth reading carefully several times: https://developer.android.com/guide/components/intents-filters#DataTest

Solution:

    <!-- 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>

EDIT:

The solution above will fail to match a file that has a b in the path before bloomd. See this answer for details on why and how to overcome that.