Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 377
Brais Gabin Avatar asked Sep 18 '12 09:09

Brais Gabin


People also ask

How do you get MIME type corresponding to a content URI?

To get the data type of a shared file given its content URI, the client app calls ContentResolver. getType() . This method returns the file's MIME type. By default, a FileProvider determines the file's MIME type from its filename extension.

What is URI and MIME?

The MIME type of the data URI. A data URI consists of a "media type" followed by data. The media type starts with a MIME type and can be followed by extra parameters. If the MIME type representation in the URI text contains URI escapes, they are unescaped in the returned string.

How is MIME type determined?

MIME types are defined by three attributes: language (lang), encoding (enc), and content-type (type). At least one of these attributes must be present for each type. The most commonly used attribute is type. The server frequently considers the type when deciding how to generate the response to the client.

Where is MIME type stored?

All MIME type information is stored in a database. The MIME database is located in the directory /usr/share/mime/ . The MIME database contains a large number of common MIME types, stored in the file /usr/share/mime/packages/freedesktop.


1 Answers

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.

like image 154
Kartik Domadiya Avatar answered Sep 17 '22 22:09

Kartik Domadiya