unable to pick files like pdf,doc,ppt in MARSHAMALLOW file chooser

倖福魔咒の 提交于 2019-12-31 07:16:08

问题


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.



来源:https://stackoverflow.com/questions/37002320/unable-to-pick-files-like-pdf-doc-ppt-in-marshamallow-file-chooser

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