Android中MimeType的用途
Intent-Filter中的<data>有一個mimeType . 它的作用是告訴Android系統本Activity可以處理的文件的類型。如設置為 “text/plain”表示可以處理“.txt”文件。
MimeTypeMap類
MimeTypeMap類是專門處理mimeType的類。
類說明以及方法如下:
Class Overview
Two-way map that maps MIME-types to file extensions and vice versa.
Summary
Public Methods
String
getExtensionFromMimeType(String mimeType)
Return the registered extension for the given MIME type.
static String
getFileExtensionFromUrl(String url)
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);
}
}