get Extension For Mime Type - Android android.webkit

Android examples for android.webkit:MimeTypeMap

Description

get Extension For Mime Type

Demo Code

import android.text.TextUtils;
import android.webkit.MimeTypeMap;

public class Main {

  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];
    }/*from  www.  ja v  a2s  . c o m*/

    return fileExtensionFromMimeType.toLowerCase();
  }

}

Related Tutorials