How to Determine Mime Type of File in Android

How to determine MIME type of file in android?

First and foremost, you should consider calling MimeTypeMap#getMimeTypeFromExtension(), like this:

// url = file path or whatever suitable URL you want.
public static String getMimeType(String url) {
String type = null;
String extension = MimeTypeMap.getFileExtensionFromUrl(url);
if (extension != null) {
type = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
}
return type;
}

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

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.

How to get the MimeType of a file with special character in android

You can use this method to get File extension. Then you can get MimeType from extension

In Java

public static String getExtension(String fileName) {
String encoded;
try {
encoded = URLEncoder.encode(fileName, "UTF-8").replace("+", "%20");
} catch (UnsupportedEncodingException e) {
encoded = fileName;
}

return MimeTypeMap.getFileExtensionFromUrl(encoded).toLowerCase();
}

In Kotlin

fun getExtension(fileName: String): String {
val encoded: String = try {
URLEncoder.encode(fileName, "UTF-8").replace("+", "%20")
} catch (e: Exception) {
fileName
}

return MimeTypeMap.getFileExtensionFromUrl(encoded).toLowerCase()
}

or as Kotlin extension:

fun File.getExtension(): String {
val encoded: String = try {
URLEncoder.encode(name, "UTF-8").replace("+", "%20")
} catch (e: Exception) {
name
}

return MimeTypeMap.getFileExtensionFromUrl(encoded).toLowerCase(Locale.getDefault())
}

Android - Get MIME type from file without extension

Is there still a way to get the MIME type of the file?

Not from the filename alone.

Or a way to determine the content Uri from the file path Uri?

There is not necessarily any "content Uri". You are welcome to try to find the file in MediaStore and see if, for some reason, it happens to know the MIME type. MediaStore may or may not know the MIME type, and if it does not, there is no way to determine it.

If you do have content:// Uri, use getType() on a ContentResolver to get the MIME type.

Determine Mime type of a uri in android returned by an intent?

I found a way to get the mime type of a content uri but nothing worked for other kind of uri's such as uri's of the form 'file://.....' .

To get the mime type of content uri's -

    ContentResolver cr = this.getContentResolver();
String mime = cr.getType(YOUR_CONTENT_URI);

This works only for content uri's . So for other uri's I am using MimeTypeMap to infer their mime type's from the file extension .

how to get mime type of a file,which has dot in the file name on android

How about this:

File f = new File(file);
System.out.println(new MimetypesFileTypeMap().getContentType(f));

Or

   if(url.lastIndexOf(".") != -1) {
String ext = url.substring(url.lastIndexOf(".")+1);
MimeTypeMap mime = MimeTypeMap.getSingleton();
String type = mime.getMimeTypeFromExtension(ext);
} else {
String type = null;
}

How to check/set mime type in Android

To check mime type you can use the below function and change the type of your own use

      public boolean isVideoFile(String path) {
String mimeType = URLConnection.guessContentTypeFromName(path);
System.out.println(mimeType);
return mimeType != null && mimeType.indexOf("video") == 0;

}

Will return true if the type is video on my case

Hope this helps.



Related Topics



Leave a reply



Submit