Brais Gabin
Brais Gabin

Reputation: 5935

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

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?

Upvotes: 112

Views: 84367

Answers (7)

Hamza Ali
Hamza Ali

Reputation: 170

for kotlin this function will work

fun getMime(uri : Uri, context: Context) : String?{
    val cr = context.contentResolver
    return cr.getType(uri)
}

Upvotes: 1

kulikovman
kulikovman

Reputation: 401

Return "image/jpeg" for example

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
    }
}

Upvotes: 5

Jemshit
Jemshit

Reputation: 10048

I will parrot the answer of Bhavesh with Kotlin extension and return type of 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()
    }
}

Upvotes: 1

Bhavesh Rangani
Bhavesh Rangani

Reputation: 1510

for Content Uri.

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

for File Uri.

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

for Both, works for Content as well as 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;
}

Upvotes: 22

Aaron
Aaron

Reputation: 1296

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

 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;
}

Upvotes: 38

Mohamed Ibrahim
Mohamed Ibrahim

Reputation: 851

Instead of this:

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

Do this:

String type = cR.getType(uri);

And you will get this: image/jpeg.

Upvotes: 9

Kartik Domadiya
Kartik Domadiya

Reputation: 29968

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"

cR.getType(uri);

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

Upvotes: 223

Related Questions