關於MIME TYPE描述
多用途互聯網郵件擴展(MIME,Multipurpose Internet Mail Extensions)是一個互聯網標准,它擴展了電子郵件標准,使其能夠支持非ASCII字符、二進制格式附件等多種格式的郵件消息。
內容類型(Content-Type),這個頭部領域用於指定消息的類型。一般以下面的形式出現。[type]/[subtype]
type有下面的形式。
- Text:用於標准化地表示的文本信息,文本消息可以是多種字符集和或者多種格式的;
- Multipart:用於連接消息體的多個部分構成一個消息,這些部分可以是不同類型的數據;
- Application:用於傳輸應用程序數據或者二進制數據;
- Message:用於包裝一個E-mail消息;
- Image:用於傳輸靜態圖片數據;
- Audio:用於傳輸音頻或者音聲數據;
- Video:用於傳輸動態影像數據,可以是與音頻編輯在一起的視頻數據格式。
subtype用於指定type的詳細形式。content-type/subtype配對的集合和與此相關的參數,將隨着時間而增長。為了確保這些值在一個有序而且公開的狀態下開發,MIME使用Internet Assigned Numbers Authority (IANA)作為中心的注冊機制來管理這些值。常用的subtype值如下所示:
- text/plain(純文本)
- text/html(HTML文檔)
- application/xhtml+xml(XHTML文檔)
- image/gif(GIF圖像)
- image/jpeg(JPEG圖像)【PHP中為:image/pjpeg】
- image/png(PNG圖像)【PHP中為:image/x-png】
- video/mpeg(MPEG動畫)
- application/octet-stream(任意的二進制數據)
- application/pdf(PDF文檔)
- application/msword(Microsoft Word文件)
- message/rfc822(RFC 822形式)
- multipart/alternative(HTML郵件的HTML形式和純文本形式,相同內容使用不同形式表示)
- application/x-www-form-urlencoded(使用HTTP的POST方法提交的表單)
- multipart/form-data(同上,但主要用於表單提交時伴隨文件上傳的場合)
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.
- staticMimeTypeMap
- 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 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);
- }
- }
- static {
- // The following table is based on /etc/mime.types data minus
- // chemical/* MIME types and MIME types that don't map to any
- // file extensions. We also exclude top-level domain names to
- // deal with cases like:
- //
- // mail.google.com/a/google.com
- //
- // and "active" MIME types (due to potential security issues).
- add("application/andrew-inset", "ez");
- add("application/dsptype", "tsp");
- add("application/futuresplash", "spl");
- add("application/hta", "hta");
- <span style="white-space:pre"> </span>...
如何使用:
實例代碼為SDK自帶的sample NotePad
startActivity(new Intent(Intent.ACTION_EDIT, uri));
其中uri為:content://com.google.provider.NotePad/notes/1
要啟動的activity為- <activity android:name="NoteEditor"
- android:theme="@android:style/Theme.Light"
- android:label="@string/title_note"
- android:screenOrientation="sensor"
- android:configChanges="keyboardHidden|orientation"
- >
- <!-- This filter says that we can view or edit the data of
- a single note -->
- <intent-filter android:label="@string/resolve_edit">
- <action android:name="android.intent.action.VIEW" />
- <action android:name="android.intent.action.EDIT" />
- <action android:name="com.android.notepad.action.EDIT_NOTE" />
- <category android:name="android.intent.category.DEFAULT" />
- <data android:mimeType="vnd.android.cursor.item/vnd.google.note" />
- </intent-filter>
- <!-- This filter says that we can create a new note inside
- of a directory of notes. -->
- <intent-filter>
- <action android:name="android.intent.action.INSERT" />
- <category android:name="android.intent.category.DEFAULT" />
- <data android:mimeType="vnd.android.cursor.dir/vnd.google.note" />
- </intent-filter>
- </activity>
隱形Intent如何找到其對定的Activity?
1.系統從intent中獲取道uri,得到了content://com.google.provider.NotePad/notes/1,
去掉開始的content:標識,得到com.google.provider.NotePad/notes/1,
然后獲取前面的com.google.provider.NotePad,然后就到Androidmanfest.xml中
找到authorities為com.google.provider.NotePad的provider,
然后就加載這個content provider- <provider android:name="NotePadProvider"
- android:authorities="com.google.provider.NotePad"
- />
2.然后調用NotePadProvider的gettype函數,並把上述URI傳給這個函數,
函數返回URI所對應的類型,這里返回Notes.CONTENT_ITEM_TYPE,代表一條日志記錄,
而CONTENT_ITEM_TYPE = " vnd.android.cursor.item/vnd.google.note "
- @Override
- public String getType(Uri uri) {
- switch (sUriMatcher.match(uri)) {
- case NOTES:
- return Notes.CONTENT_TYPE;
- case NOTE_ID:
- return Notes.CONTENT_ITEM_TYPE;
- default:
- throw new IllegalArgumentException("Unknown URI " + uri);
- }
- }
3.然后系統使用獲得的" vnd.android.cursor.item/vnd.google.note "和
”android.intent.action.EDIT”到androidmanfest.xml中去找匹配的activity.其中:android:authorities="com.google.provider.NotePad" 這段代碼是指定此ContentProvider的authorities,
類似於activity中的IntentFilter中action的作用,說白了就是這個ContentProvider在一個
android系統中的名字。ContentProvider在這個應用程序啟動以后,
就會永遠存在android系統中,直到卸載這個應用程序。