Pfredd
Pfredd

Reputation: 487

How to implement a custom MIME type for ACTION_OPEN_DOCUMENT intent?

I am using a ACTION_OPEN_DOCUMENT intent to launch a system file picker. The user is supposed to be selecting a "gpx" file (blabla.gpx).

I would like to have the picker only allow files with a ".gpx" extension to be selected.

There is a MIME type of "application/gpx+xml", but when I specify that in the intent's settype(), the gpx files are not selectable.

Is there a way to create a custom mime type of "application/gpx", just for use in my app? If so, how do I associate it with the ",gpx" file extension?

Maybe I could change the file extension associated with the "application/gpx+xml" mimne type?

Or, is there another way to accomplish what I want to do?

Upvotes: 1

Views: 3338

Answers (2)

Pfredd
Pfredd

Reputation: 487

I reported this to Google, and here is their response:

A coworker pointed out that application/gpx+xml is an established MIME type mapped to the file extension.gpx:

https://en.wikipedia.org/wiki/GPS_Exchange_Format

We'll consider adding this mapping to a future version of Android.

Upvotes: 0

CommonsWare
CommonsWare

Reputation: 1006584

I am using a ACTION_OPEN_DOCUMENT intent to launch a system file picker.

ACTION_OPEN_DOCUMENT is not limited to files.

I would like to have the picker only allow files with a ".gpx" extension to be selected.

ACTION_OPEN_DOCUMENT is not limited to document providers that use file extensions. And GPX-formatted content is not limited to files with .gpx extensions.

There is a MIME type of "application/gpx+xml", but when I specify that in the intent's settype(), the gpx files are not selectable.

Try it on an Android SDK emulator or a Google Pixel device. If you encounter the same problem, file a bug report.

However, bear in mind that the roster of MIME types and associated file extensions might be customized by the device manufacturer. That's why I recommend testing on an SDK emulator or Google Pixel, as those are pure Google, so any problems there are Google's responsibility. I agree that .gpx is a common file extension for GPX content, so ACTION_OPEN_DOCUMENT with application/gpx+xml should allow the user to pick that content.

Is there a way to create a custom mime type of "application/gpx", just for use in my app?

No, sorry, at least not in a way that affects ACTION_OPEN_DOCUMENT.

Maybe I could change the file extension associated with the "application/gpx+xml" mimne type?

No, sorry, other than via the bug report that I suggested.

is there another way to accomplish what I want to do?

Not really.

Upvotes: 1

Related Questions