Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android register app to open a custom extension file (also sharing it)

I am trying to let my app share and handle custom file extension and upon clicking it, it will open my app then I parse it.

I have looked into different ways like LIKE THIS for example and THIS, but clicking on the file from FileBrowser or WhatsApp for example is not detecting my app.

I am using the navigation component if it helps

I am not sure what I am doing wrong, I would appreciate it if someone have a working example.

Thanks.

This is some of the code I tried (I am testing with txt as it is an easy extension) (1)

 <intent-filter
                android:icon="@mipmap/ic_hp_launcher"
                android:label="@string/app_name"
                android:priority="999">
                <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:pathPattern="*.txt" />
            </intent-filter>

            <intent-filter
                android:icon="@mipmap/ic_hp_launcher"
                android:label="@string/app_name"
                android:priority="999">
                <action android:name="android.intent.action.VIEW" />

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

                <data android:scheme="http" />
                <data android:host="*" />
                <data android:mimeType="application/txt" />
            </intent-filter>

I also tried (2)

 <intent-filter
                android:icon="@mipmap/ic_hp_launcher"
                android:label="@string/app_name"
                android:priority="999">
                <action android:name="android.intent.action.VIEW" />
                <category android:name="android.intent.category.DEFAULT" />

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

            <intent-filter
                android:icon="@mipmap/ic_hp_launcher"
                android:label="@string/app_name"
                android:priority="999">
                <action android:name="android.intent.action.VIEW" />
                <category android:name="android.intent.category.DEFAULT" />
                <data
                    android:host="*"
                    android:mimeType="*/*"
                    android:pathPattern=".*\\.txt"
                    android:scheme="file" />
            </intent-filter>

            <intent-filter
                android:icon="@mipmap/ic_hp_launcher"
                android:label="@string/app_name"
                android:priority="999">
                <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:scheme="content" />
                <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:host="*" />
                <data android:pathPattern=".*\\.txt" />
                <data android:pathPattern=".*\\..*\\.txt" />
                <data android:pathPattern=".*\\..*\\..*\\.txt" />
                <data android:pathPattern=".*\\..*\\..*\\..*\\.txt" />
                <!-- keep going if you need more -->

            </intent-filter>

and (3)

 <intent-filter
                android:icon="@mipmap/ic_hp_launcher"
                android:label="@string/app_name"
                android:priority="999">
                <action android:name="android.intent.action.VIEW" />

                <category android:name="android.intent.category.DEFAULT" />
                <data android:scheme="file" />
                <data android:mimeType="text/plain" />
            </intent-filter>
like image 472
M.Baraka Avatar asked Jan 25 '26 16:01

M.Baraka


1 Answers

I finally my issue, I was testing multiple things at the same time. I ended up using

  <!-- this is needed for apps like whatsapp, it doesn't provide extension, so you can't add path  -->
            <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:mimeType="application/octet-stream"
                    android:scheme="content" />
            </intent-filter>

            <!-- this is needed for apps like explorer -->
            <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:host="*"
                    android:mimeType="*/*"
                    android:pathPattern=".*\\.ext"
                    android:scheme="content" />
            </intent-filter>

If you use mimeType="*/* then check logcat (no filter at all), search for content:// then you see what is the Android sending to your app, then modify the IntentFilter to match what you want. Some apps don't send the extension so using pathPattern was not working with it, Also using schema = content is needed

Thanks @CommonsWare for the help.

like image 143
M.Baraka Avatar answered Jan 27 '26 08:01

M.Baraka



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!