问题
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