androidkotlinintentfilter

Intent filter not working for file extension


I want to share / receive a file with ".wit" extension but I cannot get it to work.

First of all, I send the file (all ok).

private fun createIntent(file: File) {
    val fileUri: Uri = FileProvider.getUriForFile(context, FILE_PROVIDER, file)

    val shareIntent = Intent(Intent.ACTION_SEND)
    shareIntent.type = "text/plain"
    shareIntent.putExtra(Intent.EXTRA_STREAM, fileUri)
    shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION) // Grant read permission

    // You can add a subject and text to the sharing message if desired
    shareIntent.putExtra(Intent.EXTRA_SUBJECT, file.name)
    shareIntent.putExtra(Intent.EXTRA_TEXT, String.format(context.getString(R.string.workout_share_text), file.name))

    // Start the share activity
    context.startActivity(Intent.createChooser(shareIntent, context.getString(R.string.share_workout_content_description)))
}

companion object {
    private const val SHARED_FILE_EXTENSION = ".wit"
    private const val OUTPUT = "output"
    private const val FILE_PROVIDER = "com.workout.intervaltimer.fileprovider"
}

Then I try to receive the file, which should lead to open my app but it doesn't.

<activity
        android:name=".LauncherActivity"
        android:exported="true"
        android:theme="@style/Theme.App.Starting">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </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:host="work.out"
                android:scheme="http"/>
        </intent-filter>
        <intent-filter>
            <action android:name="android.intent.action.SEND" />
            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.BROWSABLE" />
            <data android:scheme="content" />
            <data android:scheme="file" />
            <data android:mimeType="text/plain" />
            <data android:pathPattern=".*\\.wit" />
        </intent-filter>
        <meta-data
            android:name="android.app.lib_name"
            android:value="" />
    </activity>

What I'm I doing wrong? It looks so simple. Ideas?


Solution

  • Try to recognise the format as follows:

    <intent-filter android:priority="999">
        <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:host="*" />
        <data android:mimeType="application/octet-stream" />
        <data android:pathPattern=".*\\..*\\..*\\..*\\..*\\.yourextension" />
        <data android:pathPattern=".*\\..*\\..*\\..*\\.yourextension" />
        <data android:pathPattern=".*\\..*\\..*\\.yourextension" />
        <data android:pathPattern=".*\\..*\\.yourextension" />
        <data android:pathPattern=".*\\.yourextension" />
        <data android:scheme="content" />
    </intent-filter>
    

    Also you should look: