androidandroid-manifestintentfilterfile-associationandroid-12

Is there an official way on API 31 to offer a file-association, maybe using pathSuffix/pathAdvancedPattern?


Background

It is a very known feature on modern desktop OSs to be able to handle files, allowing the user to open them from the file-manager and other apps, as "file assosiation" configuration.

The problem

So far, it wasn't such a convenient thing on Android, for both users and developers, to set an association of a file type.

Up to Android API 30 (Android 11, AKA Android R), you had to use some weird workarounds, especially if the file isn't a known one.

Example for "xyz" :

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

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

    <data android:scheme="content" />
    <data android:scheme="file" />
    <data android:host="*" />
    <data android:mimeType="*/*" />
    <data android:pathPattern=".*\\.xyz" />
    <data android:pathPattern=".*\\..*\\.xyz" />
    <data android:pathPattern=".*\\..*\\..*\\.xyz" />
    <data android:pathPattern=".*\\..*\\..*\\..*\\.xyz" />
    <data android:pathPattern=".*\\..*\\..*\\..*\\..*\\.xyz" />
    ...
</intent-filter>

And if it's a known one, such as a ZIP file, maybe something like this (not sure if it's the minimal/best one) :

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

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

    <data android:scheme="package" />
    <data android:scheme="content" />
    <data android:scheme="file" />
    <data android:mimeType="application/x-zip" />
    <data android:mimeType="application/x-zip-compressed" />
    <data android:mimeType="application/zip" />
</intent-filter>

In fact, even if it's a known one, you should still consider using both ways, as some apps handle only the first way.

But on Android API 31 (Android 12, AKA Android S) , it seems it has changed and we might be able to write less in the manifest (sadly probably only if minimal API is 31).

What I've found

The only thing I've found for this is on the docs:

Sadly, there were no examples that I could find, and I don't even know if it's the official way to handle files now.

The questions

  1. Is it now really the true, valid, official way to handle files on Android ?
  2. How should I use it now? Do I still need to set mimeType ? Will this work for files of any kind, whether known or not?
  3. Is it possible to set it up this way, and stop using the ways I've mentioned? Or this is only if I set the minSdk to be 31 ?
  4. Does it affect the experience for users, in any way ?

Solution

  • OK after some research by myself, here are my answers to my questions:

    1. Is it now really the true, valid, official way to handle files on Android ?

    Seems so.

    1. How should I use it now? Do I still need to set mimeType ? Will this work for files of any kind, whether known or not?

    Seems it's possible to just use this for the first part:

    <intent-filter>
        <action android:name="android.intent.action.VIEW" />
    
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />
        <category android:name="android.intent.category.OPENABLE" />
    
        <data android:scheme="content" />
        <data android:scheme="file" />
        <data android:host="*" />
        <data android:mimeType="*/*" />
        <data android:pathSuffix=".xyz" />
        ...
    </intent-filter>
    

    The second part stays the same because it is used in special cases.

    However, I failed to use the alternative of pathAdvancedPattern for some reason (tried using <data android:pathAdvancedPattern=".*\\.xyz" />).

    1. Is it possible to set it up this way, and stop using the ways I've mentioned? Or this is only if I set the minSdk to be 31 ?

    Sadly no. I've requested it to be a syntactic sugar from pathSuffix to pathPattern for pre-Android-12, here.

    1. Does it affect the experience for users, in any way ?

    Doesn't seem so.