Returns the MIME type for a given file name, based on its extension - Android android.webkit

Android examples for android.webkit:MimeTypeMap

Description

Returns the MIME type for a given file name, based on its extension

Demo Code

import android.provider.MediaStore;
import android.provider.MediaStore;
import android.util.Log;

public class Main{

    /**/*from w w w . ja  v  a 2  s .c om*/
     * Returns the MIME type for a given file name, based on its extension.
     * 
     * @param filename
     * @return MIME type; "" if unknown; null if filename is null.
     */
    public static String getMimeType(String filename) {
        String mimeType = null;

        if (filename == null) {
            return mimeType;
        }
        if (filename.endsWith(".3gp")) {
            mimeType = "video/3gpp";
        } else if (filename.endsWith(".mid")) {
            mimeType = "audio/mid";
        } else if (filename.endsWith(".mp3")) {
            mimeType = "audio/mpeg";
        } else if (filename.endsWith(".xml")) {
            mimeType = "text/xml";
        } else {
            Log.i("TAG", "Unknown media type of file '" + filename + "'");
            mimeType = "";
        }
        return mimeType;
    }

}

Related Tutorials