How filter JSON files with Intent.ACTION_OPEN_DOCUMENT?

和自甴很熟 提交于 2020-01-06 04:43:06

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!