Let me first say I've been reading some similar questions here, for example this one, but was only able to solve my issue partially and still having doubts.
I'm not sure how custom file registration works in Android, I don't know if a new file type can be registered on app install or it can only be done with an intent-filter in the activity that will open the file, and second, I'm not quite sure if there is any possibility to automatically associate your own custom file with your app, or this is something that only the user can do.
That said, I'm creating a custom file type (.aw extension) to be opened with my app and with the next intent-filter I can open the file from the file explorer -for example- in my Android 9 Huawei, but it's not working at all with other devices, like my old Android 5 tablet (which says "Cannot open file" on file click) or my friend Android 9 Samsung. I said that solved "partially" because it appears not to be a universal working intent-filter.
How can I find a "all Android versions" working way of registering a custom file type and let me know if there is any possibility that registration is automatically done so whenever you see a .aw file in explorer -or wherever- your icon appears and your app opens the file.
This is my intent-filter in manifest.xml
<intent-filter
android:icon="@drawable/ic_launcher"
android:label="@string/app_name">
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<data
android:host="*"
android:mimeType="application/aw"
android:scheme="content" />
</intent-filter>
And this is how I treat it in activity that will open the file:
private String[] getIntentExtras()
{
Intent intent = getIntent();
String fileContents = null;
String action = intent.getAction();
if(action!=null)
{
if (action.equals(Intent.ACTION_VIEW))
{
fileContents = AWImport.importData(intent);
if (fileContents.startsWith("whatever"))
{
fileContents = fileContents.replace("whatever", "");
}
else
{
showErrorOpeningFile();
return null;
}
}
}
else
...
I'm not sure how custom file registration works in Android
It has never worked especially well, and it is nearly pointless on modern versions of Android. Make sure that you have other ways for the user to choose content for use in your app, such as an "open" toolbar button that triggers an ACTION_OPEN_DOCUMENT
Intent
.
I don't know if a new file type can be registered on app install or it can only be done with an intent-filter in the activity that will open the file
It can only be done with an <intent-filter>
. That typically goes on the <activity>
that will handle the Intent
. It could go on an <activity-alias>
that you enable/disable separately, if there is some need for this.
I'm not quite sure if there is any possibility to autommatically associate your own custom file with your app, or this is something that only the user can do
Having the <intent-filter>
is automatic. Few apps will be creating a matching Intent
, though.
I can open the file from the file explorer -for example- in my Android 9 Huawei
Presumably, that is a bug in that device. Approximately zero apps written in human history will know about your unofficial (and arguably invalid) application/aw
MIME type to create an Intent
that uses it.
How can I find a "all Android versions" working way of registering a custom file type
There is none.
whenever you see a .aw file in explorer -or wherever- your icon appears and your app opens the file
There is no guaranteed way to do this. How a file explorer handles files is up to the developers of the file explorer, not you. Many will only work for common and popular MIME types, not others.
You are welcome to add an <intent-filter>
using pathPattern
to try to match literally on file extensions. Since a Uri
does not have to have a file extension, this will not work in all cases, but it will work for some users and some file explorers.
In general, consider this form of getting content to your app to be an additional feature, usable by a small percentage of your users. Focus primarily on something else, such as ACTION_OPEN_DOCUMENT
, that will work for everyone.