Example usage for android.webkit MimeTypeMap getSingleton

List of usage examples for android.webkit MimeTypeMap getSingleton

Introduction

In this page you can find the example usage for android.webkit MimeTypeMap getSingleton.

Prototype

public static MimeTypeMap getSingleton() 

Source Link

Document

Get the singleton instance of MimeTypeMap.

Usage

From source file:Main.java

/**
 * Return the MIME type from the URI//from w w w.ja v  a 2  s .  c  o  m
 * @param context Context
 * @param uri URI
 * @return Return the MIME type
 */
public static String getMimetypeFromUri(Context context, Uri uri) {
    String contentType = context.getContentResolver().getType(uri);
    if (TextUtils.isEmpty(contentType)) {
        final MimeTypeMap type_map = MimeTypeMap.getSingleton();
        // Get the extension from the path
        String extension = MimeTypeMap.getFileExtensionFromUrl(uri.toString());
        extension = extension.toLowerCase();
        if (extension.contains(".")) {
            extension = extension.substring(extension.lastIndexOf("."));
        }
        contentType = type_map.getMimeTypeFromExtension(extension);
    }
    return contentType;
}

From source file:Main.java

public static String getMimeTypeFromFileExtension(String extension, String defaultType) {
    MimeTypeMap mtm = MimeTypeMap.getSingleton();
    String mimetype = defaultType;

    if (extension != null) {
        String type = mtm.getMimeTypeFromExtension(extension);
        if (type != null) {
            mimetype = type;/*from  ww  w.j  a va 2  s .  c om*/
        } else {
            String lowerExtension = extension.toLowerCase();
            if (EXTRA_MIMETYPES.containsKey(lowerExtension)) {
                mimetype = EXTRA_MIMETYPES.get(lowerExtension);
            }
        }
    }

    return mimetype;
}

From source file:Main.java

public static String getFileExtensionFromMimeType(String mimeType, String defaultExtension) {
    String result = defaultExtension;
    String extension = MimeTypeMap.getSingleton().getExtensionFromMimeType(mimeType);
    if (extension != null) {
        result = extension;/*  w w w .j a  va 2  s  . c o m*/
    } else {
        for (String ext : EXTRA_MIMETYPES.keySet()) {
            if (EXTRA_MIMETYPES.get(ext).equalsIgnoreCase(mimeType)) {
                return ext;
            }
        }
    }

    return result;
}

From source file:Main.java

public static String resolveFileName(Uri uri, Activity activity) {
    String filename = null;//from  w ww. ja v  a2s . c  o  m
    String uriString = uri.toString();

    if (uriString.startsWith("content://")) {
        Cursor cursor = null;
        try {
            cursor = activity.getContentResolver().query(uri, null, null, null, null);
            if (cursor != null && cursor.moveToFirst()) {
                String mimeType = MimeTypeMap.getSingleton()
                        .getExtensionFromMimeType(activity.getContentResolver().getType(uri));
                filename = cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME));

                if (mimeType != null && !filename.endsWith("." + mimeType)) {
                    filename += "." + mimeType;
                }
            }
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }
    } else if (uriString.startsWith("file://")) {
        filename = (new File(uriString)).getName();
    }
    return filename;
}

From source file:Main.java

/**
 * To add hack to .3gp file/*w  ww .  j av  a 2 s .c  o m*/
 * @param url
 * @return
 */
public static String getAudioMimeType(String url) {
    String type = null;
    String extension = MimeTypeMap.getFileExtensionFromUrl(url);
    Log.w("KKIM", " Sending File Extension = " + extension);
    if (extension != null) {
        if (extension.equalsIgnoreCase("3gp") || extension.equalsIgnoreCase("3gpp")) {
            if (is3gpFileAudio(url)) {
                type = "audio/3gpp";
            } else {
                MimeTypeMap mime = MimeTypeMap.getSingleton();
                type = mime.getMimeTypeFromExtension(extension);
            }
        } else {
            MimeTypeMap mime = MimeTypeMap.getSingleton();
            type = mime.getMimeTypeFromExtension(extension);
        }

        if (type.equalsIgnoreCase("application/ogg")) {
            type = "audio/ogg";
            Log.d("KKIM", "Formatting Audio File Type from application/ogg to audio/ogg");
        }
    }
    return type;
}

From source file:Main.java

public static String assetMimeTypeToExtension(String mimeType) {
    MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
    String extension = mimeTypeMap.getExtensionFromMimeType(mimeType);
    if (extension == null) {
        return mimeType;
    }//from  ww  w .  j av  a2 s  .c o  m
    return extension;
}

From source file:Main.java

public static String getMimeType(String filePath) {
    String type = null;//from  w ww.  j ava  2  s .c  om
    String extension = MimeTypeMap.getFileExtensionFromUrl(filePath);
    if (extension != null) {
        type = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
    }
    return type;
}

From source file:Main.java

public static String getMimeTypeFromFileName(String fileName) {
    return MimeTypeMap.getSingleton().getMimeTypeFromExtension(getExtensionFromFileName(fileName));
}

From source file:Main.java

public static String getMimeType(final String fileName) {
    String result = "application/octet-stream";
    int extPos = fileName.lastIndexOf(".");
    if (extPos != -1) {
        String ext = fileName.substring(extPos + 1);
        result = MimeTypeMap.getSingleton().getMimeTypeFromExtension(ext);
    }/* w  w w  . java  2 s  .  co m*/
    return result;
}

From source file:Main.java

/**
 * Mimetype String of a file/*from   w  w  w.j  ava  2  s. com*/
 * @param path
 * @return
 */
public static String getMimeTypeFromName(String path) {
    String extension = "";
    int pos = path.lastIndexOf('.');
    if (pos >= 0) {
        extension = path.substring(pos + 1);
    }
    String result = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension.toLowerCase());
    return (result != null) ? result : "";
}