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 plaincopy在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 plaincopy在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 plaincopy在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 plaincopy
 
 
  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 plaincopy
 
 
  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 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系统中,直到卸载这个应用程序。
原文地址:https://www.cnblogs.com/xuecanmeng/p/5258617.html