List of usage examples for android.webkit MimeTypeMap getMimeTypeFromExtension
@Nullable
public String getMimeTypeFromExtension(String extension)
From source file:com.docd.purefm.utils.MimeTypes.java
/** * It is impossible to detect whether path is a directory for sure. * Call this only if you sure the path does not point to a directory * * @param path File path/*from w w w.j ava2s . c o m*/ * @return mime type based on file extension */ @Nullable private static String getMimeType(@NonNull final String path) { if (path.endsWith(File.separator)) { return null; } String type = null; final String extension = FilenameUtils.getExtension(FilenameUtils.getName(path)); if (extension != null && !extension.isEmpty()) { final String extensionLowerCase = extension.toLowerCase(Locale.US); final MimeTypeMap mime = MimeTypeMap.getSingleton(); type = mime.getMimeTypeFromExtension(extensionLowerCase); if (type == null) { type = MIME_TYPES.get(extensionLowerCase); } } return type; }
From source file:org.zywx.wbpalmstar.engine.EUtil.java
public static void installApp(Context context, String inAppPath) { if (null == inAppPath || 0 == inAppPath.trim().length()) { return;/*from w w w .j a v a 2s . co m*/ } String reallyPath = ""; File file = new File(inAppPath); if (file.exists()) { reallyPath = inAppPath; } else { reallyPath = copyFileToStorage(context, inAppPath); if (null == reallyPath) { return; } } // install apk. Intent intent = new Intent(Intent.ACTION_VIEW); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); MimeTypeMap type = MimeTypeMap.getSingleton(); String mime = type.getMimeTypeFromExtension("apk"); reallyPath = reallyPath.contains("file://") ? reallyPath : ("file://" + reallyPath); intent.setDataAndType(Uri.parse(reallyPath), mime); context.startActivity(intent); }
From source file:com.github.chenxiaolong.dualbootpatcher.pathchooser.PathChooserDialog.java
private static boolean isMimeType(@NonNull File file, @Nullable String filter, @NonNull MimeTypeMap mimeTypeMap) { if (filter != null) { String extension = FilenameUtils.getExtension(file.getName()); String mimeType = mimeTypeMap.getMimeTypeFromExtension(extension); return mimeMatches(filter, mimeType); }//from w w w . ja v a 2 s .c om return true; }
From source file:com.karura.framework.plugins.Capture.java
/** * Looks up the mime type of a given file name. * //from ww w. j a va 2s. co m * @param filename * @return a mime type */ @SuppressLint("DefaultLocale") public static String getMimeType(String filename) { if (filename != null) { // Stupid bug in getFileExtensionFromUrl when the file name has a space // So we need to replace the space with a url encoded %20 String url = filename.replace(" ", "%20").toLowerCase(); MimeTypeMap map = MimeTypeMap.getSingleton(); String extension = MimeTypeMap.getFileExtensionFromUrl(url); if (extension.toLowerCase().equals("3ga")) { return AUDIO_3GPP; } else { return map.getMimeTypeFromExtension(extension); } } else { return ""; } }
From source file:mil.nga.giat.mage.sdk.utils.MediaUtility.java
public static String getMimeType(String url) { String type = null;// w w w . j a v a 2 s .c o m if (StringUtils.isBlank(url)) { return type; } String extension = null; try { extension = MimeTypeMap.getFileExtensionFromUrl(URLEncoder.encode(url.replaceAll("\\s*", ""), "UTF-8")); } catch (UnsupportedEncodingException uee) { Log.e(LOG_NAME, "Unable to determine file extension"); } if (StringUtils.isBlank(extension)) { int i = url.lastIndexOf('.'); if (i > 0 && url.length() >= i + 1) { extension = url.substring(i + 1); } } if (!StringUtils.isBlank(extension)) { MimeTypeMap mime = MimeTypeMap.getSingleton(); type = mime.getMimeTypeFromExtension(extension); } return type; }
From source file:mobisocial.musubi.objects.FileObj.java
public static Obj from(Context context, Uri dataUri) throws IOException { //TODO: is this the proper way to do it? if (dataUri == null) { throw new NullPointerException(); }//from www . j a va2s . c o m ContentResolver cr = context.getContentResolver(); InputStream in = cr.openInputStream(dataUri); long length = in.available(); String ext; String mimeType; String filename; MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton(); if ("content".equals(dataUri.getScheme())) { ContentResolver resolver = context.getContentResolver(); mimeType = resolver.getType(dataUri); ext = mimeTypeMap.getExtensionFromMimeType(mimeType); filename = "Musubi-" + sDateFormat.format(new Date()); } else { ext = MimeTypeMap.getFileExtensionFromUrl(dataUri.toString()); mimeType = mimeTypeMap.getMimeTypeFromExtension(ext); filename = Uri.parse(dataUri.toString()).getLastPathSegment(); if (filename == null) { filename = "Musubi-" + sDateFormat.format(new Date()); } } if (mimeType == null || mimeType.isEmpty()) { throw new IOException("Unidentified mime type"); } if (ext == null || ext.isEmpty()) { ext = mimeTypeMap.getExtensionFromMimeType(mimeType); } if (!ext.isEmpty() && !filename.endsWith(ext)) { filename = filename + "." + ext; } if (mimeType.startsWith("video/")) { return VideoObj.from(context, dataUri, mimeType); } else if (mimeType.startsWith("image/")) { return PictureObj.from(context, dataUri, true); } if (length > EMBED_SIZE_LIMIT) { if (length > CORRAL_SIZE_LIMIT) { throw new IOException("file too large for push"); } else { return fromCorral(context, mimeType, filename, length, dataUri); } } else { in = cr.openInputStream(dataUri); return from(mimeType, filename, length, IOUtils.toByteArray(in)); } }
From source file:com.applozic.mobicommons.file.FileUtils.java
public static String getMimeType(String url) { String type = null;/*from ww w . j av a 2 s .c om*/ String extension = MimeTypeMap.getFileExtensionFromUrl(url); if (TextUtils.isEmpty(extension) && url.contains(".")) { extension = url.substring(url.lastIndexOf('.') + 1).toLowerCase(); } if (!TextUtils.isEmpty(extension)) { MimeTypeMap mime = MimeTypeMap.getSingleton(); type = mime.getMimeTypeFromExtension(extension.toLowerCase()); } return type; }
From source file:com.pheromone.plugins.FileUtils.java
/** * Looks up the mime type of a given file name. * * @param filename//from w w w . j av a2 s .c o m * @return a mime type */ public static String getMimeType(String filename) { MimeTypeMap map = MimeTypeMap.getSingleton(); return map.getMimeTypeFromExtension(map.getFileExtensionFromUrl(filename)); }
From source file:com.MustacheMonitor.MustacheMonitor.FileUtils.java
/** * Looks up the mime type of a given file name. * * @param filename//from w w w . ja v a2 s . co m * @return a mime type */ public static String getMimeType(String filename) { MimeTypeMap map = MimeTypeMap.getSingleton(); return map.getMimeTypeFromExtension(MimeTypeMap.getFileExtensionFromUrl(filename)); }
From source file:com.github.vase4kin.teamcityapp.artifact.router.ArtifactRouterImpl.java
/** * {@inheritDoc}/* ww w . ja va2s .c o m*/ */ @Override public void startFileActivity(File file) { MimeTypeMap map = MimeTypeMap.getSingleton(); String ext = MimeTypeMap.getFileExtensionFromUrl(file.getName()); String type = map.getMimeTypeFromExtension(ext); if (type == null) { type = ALL_FILES_TYPE; } Intent intent = new Intent(Intent.ACTION_VIEW); Uri data = FileProvider.getUriForFile(mActivity, BuildConfig.APPLICATION_ID + ".provider", file); intent.setDataAndType(data, type); intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION); //User couldn't have app with type intent try { mActivity.startActivity(intent); } catch (android.content.ActivityNotFoundException e) { intent.setDataAndType(data, ALL_FILES_TYPE); mActivity.startActivity(intent); } }