Android 如何获取具有 Uri 的文件的 MIME 类型?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12473851/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 10:45:58  来源:igfitidea点击:

How I can get the mime type of a file having its Uri?

androidurimime-types

提问by Brais Gabin

I have a List of Uris obtained with the Gallery and the Camera. These Uris are like this: content://media/external/images/media/94. How I can get its mime type?

我有一个通过画廊和相机获得的 Uris 列表。这些 Uris 是这样的:content://media/external/images/media/94. 我怎样才能得到它的 mime 类型?

回答by Kartik Domadiya

You can try

你可以试试

ContentResolver cR = context.getContentResolver();
MimeTypeMap mime = MimeTypeMap.getSingleton();
String type = mime.getExtensionFromMimeType(cR.getType(uri));

Edit :

编辑 :

mime.getExtensionFromMimeType(cR.getType(uri)) 

returns -> "jpeg"

返回 -> "jpeg"

cR.getType(uri);

returns "image/jpeg" that is the expected value.

返回“图像/jpeg”这是预期值。

回答by Aaron

This method returns the extension of the file (jpg, png, pdf, epub etc..).

此方法返回文件的扩展名(jpg、png、pdf、epub 等)。

 public static String getMimeType(Context context, Uri uri) {
    String extension;

    //Check uri format to avoid null
    if (uri.getScheme().equals(ContentResolver.SCHEME_CONTENT)) {
        //If scheme is a content
        final MimeTypeMap mime = MimeTypeMap.getSingleton();
        extension = mime.getExtensionFromMimeType(context.getContentResolver().getType(uri));
    } else {
        //If scheme is a File
        //This will replace white spaces with %20 and also other special characters. This will avoid returning null values on file name with spaces and special characters.
        extension = MimeTypeMap.getFileExtensionFromUrl(Uri.fromFile(new File(uri.getPath())).toString());

    }

    return extension;
}

回答by Bhavesh Rangani

for ContentUri.

ContentUri

ContentResolver cr = context.getContentResolver();
mimeType = cr.getType(contentUri);

for FileUri.

FileUri

String fileExtension = MimeTypeMap.getFileExtensionFromUrl(fileUri
            .toString());
mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(
            fileExtension.toLowerCase());

for Both, works for Contentas well as File.

在这两方面,工程Content以及File

public String getMimeType(Context context, Uri uri) {
    String mimeType = null;
    if (ContentResolver.SCHEME_CONTENT.equals(uri.getScheme())) {
        ContentResolver cr = context.getContentResolver();
        mimeType = cr.getType(uri);
    } else {
        String fileExtension = MimeTypeMap.getFileExtensionFromUrl(uri
                .toString());
        mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(
                fileExtension.toLowerCase());
    }
    return mimeType;
}

回答by Mohamed Ibrahim

Instead of this:

取而代之的是:

String type = mime.getExtensionFromMimeType(cR.getType(uri));

Do this:

做这个:

String type = cR.getType(uri);

And you will get this: image/jpeg.

你会得到这个:image/jpeg