Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How filter JSON files with Intent.ACTION_OPEN_DOCUMENT?

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?

like image 669
user1785730 Avatar asked Oct 22 '25 19:10

user1785730


1 Answers

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.

like image 133
Vikalp Patel Avatar answered Oct 24 '25 10:10

Vikalp Patel