问题
When I try to open a .json file with
startActivityForResult(Intent(Intent.ACTION_OPEN_DOCUMENT).apply {
addCategory(Intent.CATEGORY_OPENABLE)
type = "application/json"
}, 0)
the chooser lists files of all types instead of just .json files. And what's worth, all files are greyed out, I can't select any, not even my .json files.
However, when I change application/json to */*, the files aren't greyed out and I can open them.
With ACTION_GET_CONTENT instead of ACTION_OPEN_DOCUMENT the behaviour is the same.
Why doesn't the type setting work as expected? How to make it work?
回答1:
Android does not support json as a MIME type. You can check out the source codes of MimeUtils.
MimeTypeMap.getSingleton().getMimeTypeFromExtension("json");
returns
null;
You can use "application/octet-stream" instead of "application/json"
. It will show "*.json files among other files though it will filters out images, videos, music & text files.
来源:https://stackoverflow.com/questions/58055318/how-filter-json-files-with-intent-action-open-document