Search code examples
androidandroid-intentintentfilter

Issue with intent filter for opening a file in Android app


I'm trying to open any file with a .conf extension in my android app. Here's what I have in my AndroidManifest.xml:

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

But, when I tap on a .conf file in the Downlaods folder, it says "Can't open file."

Here's what I get when I use Intent Intercept:

enter image description here

What am I doing wrong?


Solution

  • Remove:

    <data android:host="*" />
    

    from both <intent-filter> entries and:

    <data android:pathPattern=".*\\.conf" />
    

    from the content one.

    File extensions are not used much on Android. Starting with Android Q, files are not used much on Android. There is no requirement for a ContentProvider to put a file-like extension on a content Uri, as you can see from the Uri in your screenshot.

    If you wish to support common Intent actions like ACTION_VIEW, your best bet is to save the file in a common meta-format (e.g., JSON, XML) with a file extension that matches, then have your <intent-filter> filter on the corresponding MIME type. You will need to deal with the possibility that the user chooses a file that was not created by your app, though technically you need to deal with that even with your custom extension.