Java tutorial
//package com.java2s; /* * Copyright (C) 2011 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import android.text.TextUtils; import android.webkit.MimeTypeMap; public class Main { /** * Helper to convert unknown or unmapped attachments to something useful based on filename * extensions. The mime type is inferred based upon the table below. It's not perfect, but * it helps. * * <pre> * |---------------------------------------------------------| * | E X T E N S I O N | * |---------------------------------------------------------| * | .eml | known(.png) | unknown(.abc) | none | * | M |-----------------------------------------------------------------------| * | I | none | msg/rfc822 | image/png | app/abc | app/oct-str | * | M |-------------| (always | | | | * | E | app/oct-str | overrides | | | | * | T |-------------| | |-----------------------------| * | Y | text/plain | | | text/plain | * | P |-------------| |-------------------------------------------| * | E | any/type | | any/type | * |---|-----------------------------------------------------------------------| * </pre> * * NOTE: Since mime types on Android are case-*sensitive*, return values are always in * lower case. * * @param fileName The given filename * @param mimeType The given mime type * @return A likely mime type for the attachment */ public static String inferMimeType(final String fileName, final String mimeType) { String resultType = null; String fileExtension = getFilenameExtension(fileName); boolean isTextPlain = "text/plain".equalsIgnoreCase(mimeType); if ("eml".equals(fileExtension)) { resultType = "message/rfc822"; } else { boolean isGenericType = isTextPlain || "application/octet-stream".equalsIgnoreCase(mimeType); // If the given mime type is non-empty and non-generic, return it if (isGenericType || TextUtils.isEmpty(mimeType)) { if (!TextUtils.isEmpty(fileExtension)) { // Otherwise, try to find a mime type based upon the file extension resultType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(fileExtension); if (TextUtils.isEmpty(resultType)) { // Finally, if original mimetype is text/plain, use it; otherwise synthesize resultType = isTextPlain ? mimeType : "application/" + fileExtension; } } } else { resultType = mimeType; } } // No good guess could be made; use an appropriate generic type if (TextUtils.isEmpty(resultType)) { resultType = isTextPlain ? "text/plain" : "application/octet-stream"; } return resultType.toLowerCase(); } /** * Extract and return filename's extension, converted to lower case, and not including the "." * * @return extension, or null if not found (or null/empty filename) */ public static String getFilenameExtension(String fileName) { String extension = null; if (!TextUtils.isEmpty(fileName)) { int lastDot = fileName.lastIndexOf('.'); if ((lastDot > 0) && (lastDot < fileName.length() - 1)) { extension = fileName.substring(lastDot + 1).toLowerCase(); } } return extension; } }