开发者

android Howto "really" get the mime type of sd card files

hi

Dont say this is a duplicate question because i have read them all for two days. And non of them really supply a useful answer.

I have a number of different files in a folder on the sdcard. Want to open right app depending on apk, txt, mp3, avi, jpg or show a chooser dialog. Therefor i need the mime type on the fly.

I have tried : manageQuery return null cursor to often.

MimeTypeMap cannot resolve the .3gp file and think its a .txt

URLConnection.guessContentTypeFromName Dont work at all

Was thinking i have to use the MediaFile.java and build my own..file.equals(".mp3")

addFileType("MP3", FILE_TYPE_MP3, "audio/mpeg");

addFileType("M4A", FILE_TYPE_M4A, "audio开发者_如何学编程/mp4");

addFileType("WAV", FILE_TYPE_WAV, "audio/x-wav");

addFileType("AMR", FILE_TYPE_AMR, "audio/amr");

Is that the way to do it?


The problem will always be twofold. First, Android's decision to have content:// uri means we have to deal with that and with file:// uri. Second, no mime identifier class will ever cover the whole ground.

So if you are only dealing with a few strange mime types, you have to test for them yourself if the mimetypemap or other identifier class does not work. If you are working with a file manager type app as I am, you might find it easier to create a class that checks by file uri and then by content uri and then does its own analysis. In this way, you can tweak the result over time and, for the most part, overcome the two basic problems above.

I am certain the original poster figured this out. But maybe this will help future seekers. I think it is important for independent developers to drive file handling back towards canonical posix file:// uri. We should deal with the Google-induced uri of content:// only as much as we have to. It was a poor and wrongly-motivated choice on their part to introduce these.


MimeTypeMap is the best you can get and then for the rest keep building upon the mime types manually.
And i'm wondering how did 3gp file was interpretted as txt? for me it gives a mime type video/3gp.
And then you can determine if it video or audio and play it accordingly.

But MimeTypeMap will reduce most of your efforts and it is the right choice.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜