Android examples for android.webkit:MimeTypeMap
get Media File Mime Type
import java.io.DataInputStream; import java.io.File; import java.io.FileInputStream; import java.io.InputStream; import java.net.URL; import java.net.URLConnection; import android.graphics.BitmapFactory; import android.net.Uri; import android.text.TextUtils; public class Main { public static String getMediaFileMimeType(File mediaFile) { String originalFileName = mediaFile.getName().toLowerCase(); String mimeType = null;//getUrlMimeType(originalFileName); if (TextUtils.isEmpty(mimeType)) { try {/*w w w. j a v a2 s . c o m*/ String filePathForGuessingMime; if (mediaFile.getPath().contains("://")) { filePathForGuessingMime = Uri.encode(mediaFile.getPath(), ":/"); } else { filePathForGuessingMime = "file://" + Uri.encode(mediaFile.getPath(), "/"); } URL urlForGuessingMime = new URL(filePathForGuessingMime); URLConnection uc = urlForGuessingMime.openConnection(); String guessedContentType = uc.getContentType(); // internally calls guessContentTypeFromName(url.getFile()); // and guessContentTypeFromStream(is); // check if returned "content/unknown" if (!TextUtils.isEmpty(guessedContentType) && !guessedContentType.equals("content/unknown")) { mimeType = guessedContentType; } } catch (Exception e) { } } // No mimeType yet? Try to decode the image and get the mimeType from there if (TextUtils.isEmpty(mimeType)) { try { DataInputStream inputStream = new DataInputStream(new FileInputStream(mediaFile)); String mimeTypeFromStream = getMimeTypeOfInputStream(inputStream); if (!TextUtils.isEmpty(mimeTypeFromStream)) { mimeType = mimeTypeFromStream; } inputStream.close(); } catch (Exception e) { } } if (TextUtils.isEmpty(mimeType)) { mimeType = ""; } else { if (mimeType.equalsIgnoreCase("video/mp4v-es")) { // Fixes #533. See: http://tools.ietf.org/html/rfc3016 mimeType = "video/mp4"; } } return mimeType; } public static String getMimeTypeOfInputStream(InputStream stream) { BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeStream(stream, null, options); return options.outMimeType; } }