Determines the MIME type for a given filename. : File « File « Android






Determines the MIME type for a given filename.

    
/** Utility class containing MIME type-related helper functions. */
public class MimeUtils {

  /**
   * Determines the MIME type for a given filename.
   * 
   * @param filename
   *            The file to determine the MIME type of.
   * @return The MIME type of the file, or a wildcard if none could be
   *         determined.
   */
  public static String getType(final String filename) {
    // There does not seem to be a way to ask the OS or file itself for this
    // information, so unfortunately resorting to extension sniffing.

    int pos = filename.lastIndexOf('.');
    if (pos != -1) {
      String ext = filename.substring(filename.lastIndexOf('.') + 1,
          filename.length());

      if (ext.equalsIgnoreCase("mp3"))
        return "audio/mpeg";
      if (ext.equalsIgnoreCase("aac"))
        return "audio/aac";
      if (ext.equalsIgnoreCase("wav"))
        return "audio/wav";
      if (ext.equalsIgnoreCase("ogg"))
        return "audio/ogg";
      if (ext.equalsIgnoreCase("mid"))
        return "audio/midi";
      if (ext.equalsIgnoreCase("midi"))
        return "audio/midi";
      if (ext.equalsIgnoreCase("wma"))
        return "audio/x-ms-wma";

      if (ext.equalsIgnoreCase("mp4"))
        return "video/mp4";
      if (ext.equalsIgnoreCase("avi"))
        return "video/x-msvideo";
      if (ext.equalsIgnoreCase("wmv"))
        return "video/x-ms-wmv";

      if (ext.equalsIgnoreCase("png"))
        return "image/png";
      if (ext.equalsIgnoreCase("jpg"))
        return "image/jpeg";
      if (ext.equalsIgnoreCase("jpe"))
        return "image/jpeg";
      if (ext.equalsIgnoreCase("jpeg"))
        return "image/jpeg";
      if (ext.equalsIgnoreCase("gif"))
        return "image/gif";

      if (ext.equalsIgnoreCase("xml"))
        return "text/xml";
      if (ext.equalsIgnoreCase("txt"))
        return "text/plain";
      if (ext.equalsIgnoreCase("cfg"))
        return "text/plain";
      if (ext.equalsIgnoreCase("csv"))
        return "text/plain";
      if (ext.equalsIgnoreCase("conf"))
        return "text/plain";
      if (ext.equalsIgnoreCase("rc"))
        return "text/plain";
      if (ext.equalsIgnoreCase("htm"))
        return "text/html";
      if (ext.equalsIgnoreCase("html"))
        return "text/html";

      if (ext.equalsIgnoreCase("pdf"))
        return "application/pdf";
      if (ext.equalsIgnoreCase("apk"))
        return "application/vnd.android.package-archive";

      // Additions and corrections are welcomed.
    }
    return "*/*";
  }

}

   
    
    
    
  








Related examples in the same category

1.Read the created file and display to the screen
2.Create a Uri for files on external storage
3.Create a Uri for files on internal storage
4.File read and write
5.View/Listen to media file
6.File Copy Util
7.Copy File Thread
8.Adaptation of the class File to add some usefull functions
9.Save to Android local file system
10.Using Ini file to manage Preferences
11.File Cache
12.Format File Size
13.File Upload
14.File Name Extension Utils
15.Write and append string to file
16.Transfers required files to the the private file system part in order to be access them from C Code.
17.returns a canonical line of a obj or mtl file.
18.Trims down obj files, so that they may be parsed faster later on.
19.Format File Size:KB, MB, GB
20.Get File Contents as String
21.Read File and return CharSequence
22.Copy File
23.Read/write From FileSystem, browse Account
24.Create Path and file under External Storage
25.Get File Extension Name
26.Extract File Name From String
27.Write String to file
28.Create an output file from raw resources.
29.Copies a directory from a jar file to an external directory.
30.Reads a file from /raw/res/ and returns it as a String
31.Read Config ini file
32.Read Write File Manager
33.Copy two files
34.Get the filename of the media file and use that as the default title
35.Copy File and Directory
36.Move a file stored in the cache to the internal storage of the specified context
37.CharSequence from File
38.File Manager
39.Save Exception to a file
40.Delete a file and create directory
41.Get a list of filenames in this folder.
42.Delete Directory
43.Delete Directory 2
44.Delete Directory 3
45.Get readable folder size
46.Copy chars from a large (over 2GB) Reader to a Writer.