Android examples for Media:Media File
get Media File Mime Type
import android.app.Activity; import android.content.Context; import android.content.CursorLoader; import android.database.Cursor; import android.graphics.BitmapFactory; import android.net.Uri; import android.provider.MediaStore; import android.text.TextUtils; import android.webkit.MimeTypeMap; import org.wordpress.android.util.AppLog.T; import java.io.DataInputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Locale; import java.util.TimeZone; public class Main{ public static String getMediaFileMimeType(File mediaFile) { String originalFileName = mediaFile.getName().toLowerCase(); String mimeType = UrlUtils.getUrlMimeType(originalFileName); if (TextUtils.isEmpty(mimeType)) { try { String filePathForGuessingMime; if (mediaFile.getPath().contains("://")) { filePathForGuessingMime = Uri.encode( mediaFile.getPath(), ":/"); } else { filePathForGuessingMime = "file://" + Uri.encode(mediaFile.getPath(), "/"); }//from w w w . j ava 2 s . c o m 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 (MalformedURLException e) { AppLog.e( AppLog.T.API, "MalformedURLException while trying to guess the content type for the file here " + mediaFile.getPath() + " with URLConnection", e); } catch (IOException e) { AppLog.e(AppLog.T.API, "Error while trying to guess the content type for the file here " + mediaFile.getPath() + " with URLConnection", 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 (FileNotFoundException e) { AppLog.e(AppLog.T.API, "FileNotFoundException while trying to guess the content type for the file " + mediaFile.getPath(), e); } catch (IOException e) { AppLog.e(AppLog.T.API, "IOException while trying to guess the content type for the file " + mediaFile.getPath(), 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; } }