How can I get the mime file type from its Uri?

I have a Uris list retrieved with Gallery and camera. These uri are: content://media/external/images/media/94 . How can I get its mime type?

+54
android mime-types uri
Sep 18 '12 at 9:11
source share
3 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", which is the expected value.

+117
Sep 18 '12 at 9:19
source share

This method returns the file extension (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; } 
+16
Apr 09 '16 at 9:09
source share

Instead of this:

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

Do it:

 String type = cR.getType(uri); 

And you get the following: image/jpeg .

+6
Mar 28 '13 at 11:05
source share



All Articles