Android MimeType的用途以及所有类型
2016-04-10 10:30
459 查看
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
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类是单例模式的,既没有公有的构造方法。使用getSinglton()方法获得MimeTypeMap对象:
MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
示例:
[java] view
plain copy
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);
}
}
---------------------------------------------------------------------------------------------------------------------------
在Android-4.2中,用MimeUtils类来管理所有支持的MimeType类型
[java] view
plain copy
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为
[html] view
plaincopy
<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
[java] view
plaincopy
<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 "
[java] view
plaincopy
@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系统中,直到卸载这个应用程序。
多用途互联网邮件扩展(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
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类是单例模式的,既没有公有的构造方法。使用getSinglton()方法获得MimeTypeMap对象:
MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
示例:
[java] view
plain copy
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);
}
}
---------------------------------------------------------------------------------------------------------------------------
在Android-4.2中,用MimeUtils类来管理所有支持的MimeType类型
[java] view
plain copy
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为
[html] view
plaincopy
<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
[java] view
plaincopy
<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 "
[java] view
plaincopy
@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系统中,直到卸载这个应用程序。
相关文章推荐
- $Android中日期和时间选择器的实现
- 关于Android开发中启动应用但桌面不显示图标的问题
- Android-使用Gallery制作图片浏览册
- ANDROID中vnd.android.cursor的解释
- Android进阶-MVP
- Android Studio 2.0 稳定版新特性介绍
- ym—— Android 5.0学习之ListView升级版RecyclerView
- 这些小工具让你的Android 开发更高效
- Android 深入理解Android中的自定义属性
- Android之旅十四 android中的xml文件解析
- [Android]知识总结一:Activity
- Android Studio 入门
- android 屏幕翻转问题
- Android资源----ShapeDrawable
- Android中的Intent Filter匹配规则介绍
- Android性能优化-内存泄露的检查与处理
- android:ToolBar详解(手把手教程)
- android:代码中动态添加子布局
- Android里的网格空隙
- Android里的网格空隙