Android examples for android.media:Media File
get Media File Name
import java.io.File; import android.text.TextUtils; import android.webkit.MimeTypeMap; public class Main { public static String getMediaFileName(File mediaFile, String mimeType) { String originalFileName = mediaFile.getName().toLowerCase(); String extension = MimeTypeMap.getFileExtensionFromUrl(originalFileName); if (!TextUtils.isEmpty(extension)) // File name already has the extension in it return originalFileName; if (!TextUtils.isEmpty(mimeType)) { // try to get the extension from mimeType String fileExtension = getExtensionForMimeType(mimeType); if (!TextUtils.isEmpty(fileExtension)) { originalFileName += "." + fileExtension; }//from ww w . java2 s. c o m } return originalFileName; } public static String getExtensionForMimeType(String mimeType) { if (TextUtils.isEmpty(mimeType)) return ""; MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton(); String fileExtensionFromMimeType = mimeTypeMap.getExtensionFromMimeType(mimeType); if (TextUtils.isEmpty(fileExtensionFromMimeType)) { // We're still without an extension - split the mime type and retrieve it String[] split = mimeType.split("/"); fileExtensionFromMimeType = split.length > 1 ? split[1] : split[0]; } return fileExtensionFromMimeType.toLowerCase(); } }