Android Question Associate my app with specific file type

DIYRicey

Member
Licensed User
Longtime User
I have an app (CirelDiagrams) that analyses data provided in a specialist binary file with file extension .crl.

On my Lenovo TB3-710F tablet running Android 5.0.1 I can navigate to a file using the File Manager app, click on a file and, if it has the .crl extension, Android will open my app. I have the following code as part of the Manifest
XML:
AddActivityText(Main,
<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="file" />
<data android:host="*" />
<data android:pathPattern=".*.crl" />
<data android:mimeType="*/*" />
</intent-filter>)

I have yet to write the code in the app actually to open the file, read it and deal with the data, but at least the app opens.

On my moto g(9) play running Android 11 I can navigate to a file using the File Manager + app but if I click on a file that has the .crl extension I get a message 'App not found'. If I click on a file with, say, .cti extension I am offered a choice of apps to run, but CirelDiagrams is not listed. (It wouldn't be able to open a .cti file, but why isn't it listed along with all the other apps?)
It seems that the system is recognising the file type but cannot find my app. My app installs OK and performs other functions correctly.

On my Google Pixel 6A, running Android 16 I get the same as for my g(9) if I use File manager + to navigate to a file.
If I use Google Files to navigate to a .crl file I get the message "None of your apps can open this file"
If a click on a .cti file I am offered just three possible apps to open the file.

I have arrived at the intent filter code by mashing together stuff from other threads on this forum, so it's all a bit of a mystery! One particular question I have about the code is why the pathPattern ".*.crl" works. What does the initial . do?

Many thanks,

Martin
 

b4x-de

Active Member
Licensed User
Longtime User
The `android: pathPattern` inside a `<data>` element defines a pattern for the path part of a URI. In your case:

B4X:
<data android:pathPattern=".*.crl" />

However, this pattern is often misunderstood because Android does not use normal Java regular expressions here. Instead, it uses its own simplified pattern system.

Important rules for `pathPattern`:

- `.` means any single character
- `*` means “0 or more repetitions of the previous character”
- `.*` together therefore behaves roughly like “any sequence of characters”

So the pattern:

B4X:
.*.crl

is interpreted approximately as:

“any path ending with `.crl`”

Typical matches would be for example:

B4X:
/test.crl
/storage/emulated/0/demo.crl

In practice, however, `pathPattern` is often problematic because:

- behavior can differ depending on the URI format,
- `file://` URIs are rarely used on newer Android versions,
- modern apps usually provide `content://` URIs via a `ContentProvider`,
- many file managers no longer expose classic `file://` URIs.

Additionally:

B4X:
<data android:scheme="file" />

is often no longer sufficient on modern Android versions.

For file type recognition, it is usually more robust to use:

B4X:
<data android:mimeType="application/octet-stream" />

or a dedicated MIME type.

If you really only want to open `.crl` files, a combination of MIME type filtering plus runtime validation of the filename is usually more reliable today than relying exclusively on `pathPattern`.
 
Upvote 0

DIYRicey

Member
Licensed User
Longtime User
Thanks so much for the quick response!

If a . means any character, won't the second . mean any character? In other words
B4X:
".*.crl"
means any path ending with 'crl'. Won't I need something like
B4X:
".*\.crl"
or
B4X:
".*\\.crl"
to mean any path ending in '.crl'?
Both of these seem to work on the old tablet, by the way.

Still not managed to get anything to work on the more modern devices. The issue seems to be trying to persuade Android 11, Android 16, File Manager + , Google Files that my app really is the one to open .crl files.
 
Upvote 0
Top