Returns the file extension or an empty string iff there is no extension.
String
getMimeTypeFromExtension(String extension)
Return the MIME type for the given extension.
static MimeTypeMap
getSingleton()
Get the singleton instance of MimeTypeMap.
boolean hasExtension(String extension)
Return true if the given extension has a registered MIME type.
boolean hasMimeType(String mimeType)
Return true if the given MIME type has an entry in the map.
MimeTypeMap类是单例模式的,既没有公有的构造方法。使用getSinglton()方法获得MimeTypeMap对象:
MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
示例:
public class MainActivity extends Activity {
private String tag = "MainActivity";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
System.out.println(111);
MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
//MimeTypeMap中是否有txt的MimeType
System.out.println(mimeTypeMap.hasExtension("txt"));
System.out.println(mimeTypeMap.hasMimeType("text/html"));
//获得txt文件类型的MimeType
String extension = mimeTypeMap.getMimeTypeFromExtension("txt");
System.out.println(extension);
}
}
String
getMimeTypeFromExtension(String extension)
Return the MIME type for the given extension.
static MimeTypeMap
getSingleton()
Get the singleton instance of MimeTypeMap.
boolean hasExtension(String extension)
Return true if the given extension has a registered MIME type.
boolean hasMimeType(String mimeType)
Return true if the given MIME type has an entry in the map.
MimeTypeMap类是单例模式的,既没有公有的构造方法。使用getSinglton()方法获得MimeTypeMap对象:
MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
示例:
public class MainActivity extends Activity {
private String tag = "MainActivity";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
System.out.println(111);
MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
//MimeTypeMap中是否有txt的MimeType
System.out.println(mimeTypeMap.hasExtension("txt"));
System.out.println(mimeTypeMap.hasMimeType("text/html"));
//获得txt文件类型的MimeType
String extension = mimeTypeMap.getMimeTypeFromExtension("txt");
System.out.println(extension);
}
}
本文介绍MimeTypeMap类的使用方法,包括获取MimeTypeMap实例、检查扩展名及MIME类型是否存在、从扩展名获取MIME类型等操作。通过示例代码展示了如何在实际应用中使用这些方法。
653

被折叠的 条评论
为什么被折叠?



