您的位置:首页 > 移动开发 > Android开发

android的MIME类型

2012-06-30 09:30 429 查看
首先这是来自维基百科的MIME定义:

多用途互联网邮件扩展(MIME,Multipurpose
Internet Mail Extensions)是一个互联网标准,它扩展了电子邮件标准,使其能够支持非ASCII字符、二进制格式附件等多种格式的邮件消息。这个标准被定义在RFC
2045、RFC
2046、RFC
2047、RFC
2048、RFC
2049等RFC中。
RFC 822转变而来的RFC
2822,规定电子邮件标准并不允许在邮件消息中使用7位ASCII字符集以外的字符。正因如此,一些非英语字符消息和二进制文件,图像,声音等非文字消息都不能在电子邮件中传输。MIME规定了用于表示各种各样的数据类型的符号化方法。
此外,在万维网中使用的HTTP协议中也使用了MIME的框架。

以下内容是MIME类型的标准,摘自:RFC2046

http://tools.ietf.org/html/rfc2046

Multipurpose Internet Mail Extensions

(MIME) Part Two:

Media Types

Status of this Memo This document specifies an Internet standards track protocol for the Internet community, and requests discussion and suggestions for improvements. Please refer to the current edition of the "Internet Official Protocol Standards" (STD 1) for the standardization state and status of this protocol. Distribution of this memo is unlimited. Abstract STD 11, RFC 822 defines a message representation protocol specifying considerable detail about US-ASCII message headers, but which leaves the message content, or message body, as flat US-ASCII text. This set of documents, collectively called the Multipurpose Internet Mail Extensions, or MIME, redefines the format of messages to allow for (1) textual message bodies in character sets other than US-ASCII, (2) an extensible set of different formats for non-textual message bodies, (3) multi-part message bodies, and (4) textual header information in character sets other than US-ASCII. These documents are based on earlier work documented in RFC 934, STD 11, and RFC 1049, but extends and revises them. Because RFC 822 said so little about message bodies, these documents are largely orthogonal to (rather than a revision of) RFC 822. The initial document in this set, RFC 2045, specifies the various headers used to describe the structure of MIME messages. This second document defines the general structure of the MIME media typing system and defines an initial set of media types. The third document, RFC 2047, describes extensions to RFC 822 to allow non-US-ASCII text


this Article was translated from Pro Android 4 page 87
Copyright © 2012 by Satya Komatineni and Dave MacLean


如同一个web 站点根据 URL 返回 MIME 类型一样(允许浏览器调用相关的程序来显示具体的内容), content provider 也有根据 URI 来返回特定 MIME 类型的责任。这给数据显示增加了弹性。知道了数据的类型,你可能有不止一个程序来处理这个数据。举例来说,如果你在存储器上有一个文本文件,有很多编辑器可以用来显示这个文本文件。根据
OS 的不同,它可能会让你选择用哪个编辑器来查看此文件。

在Android 里面的 MIME 的工作方式和在 HTTP 里类似。你在 URI 里面提供给 provider 所支持的 MIME 类型,然后 provider 根据 web 中 MIME 标准返回一个由两个字符串组成的 MIME 类型识别。

你从这里发现 MIME 类型的标准: http://tools.ietf.org/html/rfc2046
根据MIME 类型的说明,一个 MIME 类型包含两个部分:一个类型和一个子类型。这里有些熟知的 MIME 类型对:

text/html

text/css

text/xml

text/vnd.curl

application/pdf

application/rtf

application/vnd.ms-excel

你可以在Internet Assigned Numbers Authority ( IANA )网站上看到所有的类型支持: h ttp://www.iana.org/assignments/media-types/

其中基本类型有

application

audio

example

image

message

model

multipart

ext

video

这些基本类型都有它的子类型。但是如果是专有的数据格式,那么子类型会以vnd 开始。举例来说,微软的 Excel 表以 vnd.ms-excel 子类型来识别,而 pdf 不是一个专有的标准,所以它的子类型没有专有前缀。

某些子类型以x- 作为前缀。这些是非标准的子类型,不需要注册。它们被认为是由两个合作机构定义的私有类型。这里有些例子:

application/x-tar

audio/x-aiff

video/x-msvideo

android 依照相似的惯例来定义 MIME 类型。 android 里面的 vnd 指那些非标准的专有的格式。为了唯一性, android 还用了类似于域名的多个关于类型和子类型的部分。此外, android 中给每个 content 的 MIME 类型包含两种形式:一个给具体的 record
,一个给多个 records 。

对于单独的record , MIME 看起来像:

vnd.android.cursor.item/vnd.yourcompanyname.contenttype

对于多个records , MIME 类型看起来像:

vnd.android.cursor.dir/vnd.yourcompanyname.contenttype

这里有一系列的例子:

//一个 note

vnd.android.cursor.item/vnd.google.note

//一个 notes 集合的目录

vnd.android.cursor.dir/vnd.google.note

MIME类型在 android 里面得到了大规模的应用,特别是在 intent 里,系统根据 MIME 数据的类型来决定调用哪个 activity 。 MIME 类型始终通过 content providers 继承自它们的 URIs 。你在使用 MIME 类型的时候要记住三点:

*基本类型和子类型代表的东西要唯一。

*如果是非标准的,专有的类型和子类型,前面需要加 vnd 。

*对于特定的需求,注意名字空间。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: