get MIME Type for target file - Android android.webkit

Android examples for android.webkit:MimeTypeMap

Description

get MIME Type for target file

Demo Code

import android.webkit.MimeTypeMap;
import java.net.URLEncoder;
import java.util.ArrayList;

public class Main {

  public static String getMIMEType(String targetFile) {
    String url = URLEncoder.encode(targetFile);
    String extention = MimeTypeMap.getFileExtensionFromUrl(url);
    String mtype = "";

    extention = extention.toLowerCase();

    mtype = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extention);
    if (mtype == null) {

      String PLANETEXT = "text/plain";
      if ((targetFile.endsWith(".c")) || (targetFile.endsWith(".cp")) || (targetFile.endsWith(".cpp"))
          || (targetFile.endsWith(".java")) || (targetFile.endsWith(".txt")) || (targetFile.endsWith(".c++"))
          || (targetFile.endsWith(".sh")) || (targetFile.endsWith(".cmake")) || (targetFile.endsWith(".ini"))
          || (targetFile.endsWith(".php")) || (targetFile.endsWith(".py")))
        mtype = PLANETEXT;//from   ww w . j  a  va 2  s.c o m
    }

    if (mtype == null)
      mtype = "";
    return mtype;
  }

}

Related Tutorials