Android MimeType的用法和幾種類型

來源:互聯網
上載者:User

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中的有一個mimeType . 它的作用是告訴Android系統本Activity可以處理的檔案的類型。如設定為 “text/plain”表示可以處理“.txt”檔案。
      MimeTypeMap類
      MimeTypeMap類是專門處理mimeType的類。

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

      [java] view plaincopy
      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 plaincopy
        1. public class MainActivity extends Activity {
        2. private String tag = "MainActivity";
        3. @Override
        4. public void onCreate(Bundle savedInstanceState) {
        5. super.onCreate(savedInstanceState);
        6. setContentView(R.layout.main);
        7. System.out.println(111);
        8. MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
        9. //MimeTypeMap中是否有txt的MimeType
        10. System.out.println(mimeTypeMap.hasExtension("txt"));
        11. System.out.println(mimeTypeMap.hasMimeType("text/html"));
        12. //獲得txt檔案類型的MimeType
        13. String extension = mimeTypeMap.getMimeTypeFromExtension("txt");
        14. System.out.println(extension);
        15. }
        16. }

          ---------------------------------------------------------------------------------------------------------------------------
          在Android-4.2中,用MimeUtils類來管理所有支援的MimeType類型[java] view plaincopy
          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. add("application/andrew-inset", "ez");
          11. add("application/dsptype", "tsp");
          12. add("application/futuresplash", "spl");
          13. add("application/hta", "hta");
          14. ... ---------------------------------------------------------------------------------------------------------------------------

            如何使用:

            執行個體代碼為SDK內建的sample NotePad

            startActivity(new Intent(Intent.ACTION_EDIT, uri));

            其中uri為:content://com.google.provider.NotePad/notes/1

            要啟動的activity為
            [html] view plaincopy
            1. android:theme="@android:style/Theme.Light"
            2. android:label="@string/title_note"
            3. android:screenOrientation="sensor"
            4. android:configChanges="keyboardHidden|orientation"
            5. >

            6. 隱形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 plaincopy
              1. android:authorities="com.google.provider.NotePad"
              2. />

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

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

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

                [java] view plaincopy
                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系統中,直到卸載這個應用程式。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.