Determine Mime type of a uri in android returned by an intent?

假装没事ソ 提交于 2019-11-30 09:47:41

I found a way to get the mime type of a content uri but nothing worked for other kind of uri's such as uri's of the form 'file://.....' .

To get the mime type of content uri's -

    ContentResolver cr = this.getContentResolver();
    String mime = cr.getType(YOUR_CONTENT_URI);

This works only for content uri's . So for other uri's I am using MimeTypeMap to infer their mime type's from the file extension .

Xar E Ahmer

if you have URI then you can get mimetype like

Uri uri = Uri.fromFile(file);
ContentResolver cR = context.getContentResolver();
String mime = cR.getType(uri);

or try // url = file path or whatever suitable URL you want.

public static String getMimeType(String url)
{
    String type = null;
    String extension = MimeTypeMap.getFileExtensionFromUrl(url);
    if (extension != null) {
        MimeTypeMap mime = MimeTypeMap.getSingleton();
        type = mime.getMimeTypeFromExtension(extension);
    }
    return type;
}

For more details see these Answers

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