get Extension For Mime Type - Android Network

Android examples for Network:MIME

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  w  w  w .  ja  v a  2 s  .  c  o m

    return fileExtensionFromMimeType.toLowerCase();
  }
}

Related Tutorials