Method 1:
Import java.util.locale;private static String getsuffix (file file) { if (file = = NULL | |!file.exists () | | | file.isdire Ctory ()) { return null; } String fileName = File.getname (); if (Filename.equals ("") | | Filename.endswith (".")) { return null; } int index = Filename.lastindexof ("."); if (Index! =-1) { return filename.substring (index + 1). toLowerCase (locale.us); } else { return null; } } public static string getmimetype (file file) { string suffix = getsuffix (file); if (suffix = = null) { return "file/*"; } String type = Mimetypemap.getsingleton (). getmimetypefromextension (suffix); if (type! = NULL | |!type.isempty ()) { return type; } return "file/*";}
Method 2:
public static string GetMimeType (String filePath) { mediametadataretriever MMR = new Mediametadataretriever (); String MIME = "Text/plain"; if (FilePath! = null) { try { mmr.setdatasource (filePath); MIME = Mmr.extractmetadata (Mediametadataretriever.metadata_key_mimetype); } catch (IllegalStateException e) { return mime; } catch (IllegalArgumentException e) { return mime;} catch (Ru Ntimeexception e) { return mime;} } return MIME;}
Two ways to get file GetMimeType on Android