How to show preview of file and open up file in its application in Android?

孤街醉人 提交于 2019-12-07 05:28:34

问题


I have a option to attach files within a application. I this I need to show users a preview or a icon for specific item type. Icons with extension .txt, .doc, .pdf, .jpeg, .mp4 need to have its own specific icons or if possible a good preview thumbnail icon. And when the user wants to view it he would be clicking on the icon or preview. I need to start a intent to view the file.

I mean a pdf should open up in a pdf reader, or a txt or doc should open in text reader, a mp3 or mp4 should open up in a audio or video player.. and the important part I want them to com back to my application when they do a back out of the view application.

The only idea in my mind is to search through the file name and manually split out the extension and do a string compare to check it against the constant string values like ".mp3" or ".mp4" etc. This is not a good idea as there are lot of extensions for image, audio and video type. Please let me know if there is any straight forward way to do this. Thank you for your help.


回答1:


You could also read the mime type of the file and do it that way. Might be more accurate than just reading the extension although both would probably work.

http://developer.android.com/reference/android/webkit/MimeTypeMap.html

guessContentTypeFromStream(InputStream)

You can then have an array list of drawables that refer to each icon.



来源:https://stackoverflow.com/questions/6447848/how-to-show-preview-of-file-and-open-up-file-in-its-application-in-android

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