0

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>
M.Baraka
  • 607
  • 9
  • 22
  • What extension are you using and how is your implemented? – javdromero Mar 19 '21 at 20:12
  • @javdromero I just updated my question and included some of the IntentFilter that I tried, I am currently testing with `txt` extension but I want to be something like `asd` a new custom extension – M.Baraka Mar 19 '21 at 20:18
  • I guess you have already tried with just `` and it's not working – javdromero Mar 19 '21 at 20:43
  • if you mean ``` ``` then it is not working either, I also tried an IntentFilter with only the data – M.Baraka Mar 19 '21 at 20:51
  • File extensions are not used much in modern versions of Android. Focus instead of `text/plain` as a MIME type for the `content` scheme, and forget about file extensions. – CommonsWare Mar 19 '21 at 21:33
  • @CommonsWare I have updated my question and added (3) do you mean something like that? Also if you think I should avoid depending on custom extension, can you please provide an example / link to check. Thank you – M.Baraka Mar 19 '21 at 22:46
  • 1
    "do you mean something like that?" -- I would also add ``, or use that instead of ``. You will not get `file` `Uri` values on modern versions of Android. "if you think I should avoid depending on custom extension" -- `.txt` is not very custom, and it is tied to a well-known MIME type. If what you are *really* trying to do is a custom MIME type, that will mostly be practical for Web requests (so, `http` and `https` schemes, more so than `content`, and `file` will be pointless). – CommonsWare Mar 19 '21 at 22:54

1 Answers1

0

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.

M.Baraka
  • 607
  • 9
  • 22