98

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

4

6 回答 6

203

你可以试试

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

编辑 :

mime.getExtensionFromMimeType(cR.getType(uri)) 

返回->“jpeg”

cR.getType(uri);

返回“image/jpeg”,即预期值。

于 2012-09-18T09:19:54.157 回答
35

此方法返回文件的扩展名(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;
}
于 2016-04-09T09:09:46.427 回答
18

Content Uri.

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

File Uri.

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

对于两者,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;
}
于 2018-03-30T09:30:28.260 回答
8

而不是这个:

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

做这个:

String type = cR.getType(uri);

你会得到这个:image/jpeg.

于 2013-03-28T11:05:14.983 回答
0

我将使用 Kotlin 扩展和返回类型模仿 Bhavesh 的答案okhttp3.MediaType

fun Uri.mimeType(contentResolver: ContentResolver)
        : MediaType? {
    if (scheme.equals(ContentResolver.SCHEME_CONTENT)) {
        // get (image/jpeg, video/mp4) from ContentResolver if uri scheme is "content://"
        return contentResolver.getType(this)?.toMediaTypeOrNull()
    } else {
        // get (.jpeg, .mp4) from uri "file://example/example.mp4"
        val fileExtension = MimeTypeMap.getFileExtensionFromUrl(toString())
        // turn ".mp4" into "video/mp4"
        return MimeTypeMap.getSingleton()
                .getMimeTypeFromExtension(fileExtension.toLowerCase(Locale.US))
                ?.toMediaTypeOrNull()
    }
}
于 2021-04-25T09:28:52.210 回答
0

例如返回“image/jpeg”

fun Uri.getMimeType(context: Context): String? {
    return when (scheme) {
        ContentResolver.SCHEME_CONTENT -> context.contentResolver.getType(this)
        ContentResolver.SCHEME_FILE -> MimeTypeMap.getSingleton().getMimeTypeFromExtension(
            MimeTypeMap.getFileExtensionFromUrl(toString()).toLowerCase(Locale.US)
        )
        else -> null
    }
}
于 2021-05-13T04:20:45.803 回答