Android MimeType的用法和幾種類型


關於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的類。

--------------------------------------------------------------------------------------------------------------------------- 類說明以及方法如下:

[java]  view plain copy 在CODE上查看代碼片 派生到我的代碼片
 
  1. Class Overview  
  2. Two-way map that maps MIME-types to file extensions and vice versa.  
  3. Summary  
  4. Public Methods  
  5. String  
  6. getExtensionFromMimeType(String mimeType)  
  7. Return the registered extension for the given MIME type.  
  8. static String  
  9. getFileExtensionFromUrl(String url)  
  10. Returns the file extension or an empty string iff there is no extension.  
  11. String  
  12. getMimeTypeFromExtension(String extension)  
  13. Return the MIME type for the given extension.  
  14. staticMimeTypeMap  
  15. getSingleton()  
  16. Get the singleton instance of MimeTypeMap.  
  17. boolean  
  18. hasExtension(String extension)  
  19. Return true if the given extension has a registered MIME type.  
  20. boolean  
  21. hasMimeType(String mimeType)  
  22. Return true if the given MIME type has an entry in the map.  
MimeTypeMap類是單例模式的,既沒有公有的構造方法。使用getSinglton()方法獲得MimeTypeMap對象:
       MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();

示例:

[java]  view plain copy 在CODE上查看代碼片 派生到我的代碼片
 
  1. public class MainActivity extends Activity {  
  2.       private String tag = "MainActivity";  
  3.        
  4.     @Override  
  5.     public void onCreate(Bundle savedInstanceState) {  
  6.         super.onCreate(savedInstanceState);  
  7.         setContentView(R.layout.main);  
  8.         System.out.println(111);  
  9.         MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();  
  10.          
  11.         //MimeTypeMap中是否有txt的MimeType  
  12.         System.out.println(mimeTypeMap.hasExtension("txt"));  
  13.          
  14.         System.out.println(mimeTypeMap.hasMimeType("text/html"));  
  15.         //獲得txt文件類型的MimeType  
  16.         String extension = mimeTypeMap.getMimeTypeFromExtension("txt");  
  17.         System.out.println(extension);  
  18.     }  
  19. }  

 

---------------------------------------------------------------------------------------------------------------------------
在Android-4.2中,用MimeUtils類來管理所有支持的MimeType類型
[java]  view plain copy 在CODE上查看代碼片 派生到我的代碼片
 
  1. static {  
  2.         // The following table is based on /etc/mime.types data minus  
  3.         // chemical/* MIME types and MIME types that don't map to any  
  4.         // file extensions. We also exclude top-level domain names to  
  5.         // deal with cases like:  
  6.         //  
  7.         // mail.google.com/a/google.com  
  8.         //  
  9.         // and "active" MIME types (due to potential security issues).  
  10.   
  11.         add("application/andrew-inset""ez");  
  12.         add("application/dsptype""tsp");  
  13.         add("application/futuresplash""spl");  
  14.         add("application/hta""hta");  
  15. <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為    
[html]  view plain copy
 
 
  1. <activity android:name="NoteEditor"  
  2.             android:theme="@android:style/Theme.Light"  
  3.             android:label="@string/title_note"  
  4.             android:screenOrientation="sensor"  
  5.             android:configChanges="keyboardHidden|orientation"  
  6.         >  
  7.             <!-- This filter says that we can view or edit the data of  
  8.                  a single note -->  
  9.             <intent-filter android:label="@string/resolve_edit">  
  10.                 <action android:name="android.intent.action.VIEW" />  
  11.                 <action android:name="android.intent.action.EDIT" />  
  12.                 <action android:name="com.android.notepad.action.EDIT_NOTE" />  
  13.                 <category android:name="android.intent.category.DEFAULT" />  
  14.                 <data android:mimeType="vnd.android.cursor.item/vnd.google.note" />  
  15.             </intent-filter>  
  16.             <!-- This filter says that we can create a new note inside  
  17.                  of a directory of notes. -->  
  18.             <intent-filter>  
  19.                 <action android:name="android.intent.action.INSERT" />  
  20.                 <category android:name="android.intent.category.DEFAULT" />  
  21.                 <data android:mimeType="vnd.android.cursor.dir/vnd.google.note" />  
  22.             </intent-filter>  
  23.  </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
    
[java]  view plain copy
 
 
  1. <provider android:name="NotePadProvider"  
  2.     android:authorities="com.google.provider.NotePad"  
  3. />  


  2.然后調用NotePadProvider的gettype函數,並把上述URI傳給這個函數,

    函數返回URI所對應的類型,這里返回Notes.CONTENT_ITEM_TYPE,代表一條日志記錄,

    而CONTENT_ITEM_TYPE = " vnd.android.cursor.item/vnd.google.note "

 

[java]  view plain copy
 
 
  1.    @Override  
  2.     public String getType(Uri uri) {  
  3.         switch (sUriMatcher.match(uri)) {  
  4.         case NOTES:  
  5.             return Notes.CONTENT_TYPE;  
  6.         case NOTE_ID:  
  7.             return Notes.CONTENT_ITEM_TYPE;  
  8.         default:  
  9.             throw new IllegalArgumentException("Unknown URI " + uri);  
  10.         }  
  11. }  

 

  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系統中,直到卸載這個應用程序。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM