aga aga - 2 months ago 26
Android Question

The file with the .gpx extension can not be loaded via Storage Access Framework

Wikipedia says that the MIME-type of the GPX files is

application/gpx+xml
. So I used the following code:

startActivityForResult(
new Intent(Intent.ACTION_OPEN_DOCUMENT)
.addCategory(Intent.CATEGORY_OPENABLE)
.setType("application/gpx"),
READ_GPX_FILE_REQUEST_CODE
);


Also tried this one:

startActivityForResult(
new Intent(Intent.ACTION_OPEN_DOCUMENT)
.addCategory(Intent.CATEGORY_OPENABLE)
.setType("application/gpx+xml"),
READ_GPX_FILE_REQUEST_CODE
);


And as a last resort I tried to use
Intent#setTypeAndNormalize(String)
method. No luck - when the file picker dialog starts, all the files with
.gpx
extension have the grey color, i.e. they aren't selectable. Is there some issue in my code, or the SAF's
Intent
have some set of preconfigured mime-types which it can use and
application/gpx+xml
does not belong to this set?

Answer

It is up to the document provider to determine the MIME types of the documents that it provides. For some document providers, they may know document MIME types from upstream sources (e.g., an email client that offers a document provider might take the MIME types of attachments from the MIME headers of the email). For everything else, they will tend to rely upon MimeTypeMap, which in turn relies on an internal class named MimeUtils.

And MimeUtils has nothing for GPX, at least through Android 7.0.

So, you would need to take greenapps' suggestion, use */*, and be prepared to deal with parsing errors.

FWIW, I filed a feature request to get GPX added to MimeUtils.