unable to pick files like pdf,doc,ppt in MARSHAMAL

2019-03-06 14:56发布

问题:

I am uploading files like pdf,ppt,doc etc to server using from my android app but in marshmallow when file chooser opens and i browse my Sdcard or internal storage there are two problmes :
1. it shows all files like images,videos and documents etc which i cannot pick but i used intent type as application/pdf; appkication/ppt etc.,so it should let me pick those files.
2. secondly when i use external file manager like ES file explorer etc it then show all files like images,videos,documents,apks and this time it lets me choose any type of file
here is my code for file chooser

Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
            intent.setType("application/pdf;application/docx;application/xlsx;application/pptx;application/pptx;application/txt");
            startActivityForResult(intent, PICK_FILE_REQUEST);  

i am using marshmallow and i think it happens only in marshmallow as i dont have other devices earby to test this code on so help me here Thanks

i tried it like this

intent.setType("application/pdf|application/docx|application/xlsx|application/pptx|application/txt");

but it didnt let me chose any file but if i use only one MIME type like pdf or docx then it works and let me pick pdf or docx and i searched across internet and found this way to use multiple MIME type that is by using | instead of ; and for most of users it works fine but dont for me.

回答1:

setType() does not take a pipe-delimited list of types, or a semicolon-delimited list of types. It takes a single MIME type.

On API Level 19+ devices, you are welcome to add EXTRA_MIME_TYPES to your Intent (a String[] of additional MIME types). In theory, ACTION_GET_CONTENT implementations should honor this. In practice, I expect that few would. ACTION_OPEN_DOCUMENT may be more reliable, in that Android implements that itself, whereas ACTION_GET_CONTENT relies upon app developer implementations.

Since your Intent is for ACTION_GET_CONTENT (i.e., you are requesting content), you can use application/*, though this will get you some file types that you cannot support.

Otherwise, only ask for one MIME type at a time.