FileDocCategorySizeDatePackage
MimeTypeMap.javaAPI DocAndroid 5.1 API6255Thu Mar 12 22:22:10 GMT 2015android.webkit

MimeTypeMap

public class MimeTypeMap extends Object
Two-way map that maps MIME-types to file extensions and vice versa.

See also {@link java.net.URLConnection#guessContentTypeFromName} and {@link java.net.URLConnection#guessContentTypeFromStream}. This class and {@code URLConnection} share the same MIME-type database.

Fields Summary
private static final MimeTypeMap
sMimeTypeMap
Constructors Summary
private MimeTypeMap()


      
    
Methods Summary
public java.lang.StringgetExtensionFromMimeType(java.lang.String mimeType)
Return the registered extension for the given MIME type. Note that some MIME types map to multiple extensions. This call will return the most common extension for the given MIME type.

param
mimeType A MIME type (i.e. text/plain)
return
The extension for the given MIME type or null iff there is none.

        return MimeUtils.guessExtensionFromMimeType(mimeType);
    
public static java.lang.StringgetFileExtensionFromUrl(java.lang.String url)
Returns the file extension or an empty string iff there is no extension. This method is a convenience method for obtaining the extension of a url and has undefined results for other Strings.

param
url
return
The file extension of the given url.

        if (!TextUtils.isEmpty(url)) {
            int fragment = url.lastIndexOf('#");
            if (fragment > 0) {
                url = url.substring(0, fragment);
            }

            int query = url.lastIndexOf('?");
            if (query > 0) {
                url = url.substring(0, query);
            }

            int filenamePos = url.lastIndexOf('/");
            String filename =
                0 <= filenamePos ? url.substring(filenamePos + 1) : url;

            // if the filename contains special characters, we don't
            // consider it valid for our matching purposes:
            if (!filename.isEmpty() &&
                Pattern.matches("[a-zA-Z_0-9\\.\\-\\(\\)\\%]+", filename)) {
                int dotPos = filename.lastIndexOf('.");
                if (0 <= dotPos) {
                    return filename.substring(dotPos + 1);
                }
            }
        }

        return "";
    
public java.lang.StringgetMimeTypeFromExtension(java.lang.String extension)
Return the MIME type for the given extension.

param
extension A file extension without the leading '.'
return
The MIME type for the given extension or null iff there is none.

        return MimeUtils.guessMimeTypeFromExtension(extension);
    
public static android.webkit.MimeTypeMapgetSingleton()
Get the singleton instance of MimeTypeMap.

return
The singleton instance of the MIME-type map.

        return sMimeTypeMap;
    
public booleanhasExtension(java.lang.String extension)
Return true if the given extension has a registered MIME type.

param
extension A file extension without the leading '.'
return
True iff there is an extension entry in the map.

        return MimeUtils.hasExtension(extension);
    
public booleanhasMimeType(java.lang.String mimeType)
Return true if the given MIME type has an entry in the map.

param
mimeType A MIME type (i.e. text/plain)
return
True iff there is a mimeType entry in the map.

        return MimeUtils.hasMimeType(mimeType);
    
private static java.lang.StringmimeTypeFromExtension(java.lang.String extension)

        return MimeUtils.guessMimeTypeFromExtension(extension);
    
java.lang.StringremapGenericMimeType(java.lang.String mimeType, java.lang.String url, java.lang.String contentDisposition)
If the given MIME type is null, or one of the "generic" types (text/plain or application/octet-stream) map it to a type that Android can deal with. If the given type is not generic, return it unchanged.

param
mimeType MIME type provided by the server.
param
url URL of the data being loaded.
param
contentDisposition Content-disposition header given by the server.
return
The MIME type that should be used for this data.

        // If we have one of "generic" MIME types, try to deduce
        // the right MIME type from the file extension (if any):
        if ("text/plain".equals(mimeType) ||
                "application/octet-stream".equals(mimeType)) {

            // for attachment, use the filename in the Content-Disposition
            // to guess the mimetype
            String filename = null;
            if (contentDisposition != null) {
                filename = URLUtil.parseContentDisposition(contentDisposition);
            }
            if (filename != null) {
                url = filename;
            }
            String extension = getFileExtensionFromUrl(url);
            String newMimeType = getMimeTypeFromExtension(extension);
            if (newMimeType != null) {
                mimeType = newMimeType;
            }
        } else if ("text/vnd.wap.wml".equals(mimeType)) {
            // As we don't support wml, render it as plain text
            mimeType = "text/plain";
        } else {
            // It seems that xhtml+xml and vnd.wap.xhtml+xml mime
            // subtypes are used interchangeably. So treat them the same.
            if ("application/vnd.wap.xhtml+xml".equals(mimeType)) {
                mimeType = "application/xhtml+xml";
            }
        }
        return mimeType;