Android Basic- URI&MIME

mimeMIME类型就是设定某种扩展名的文件用一种应用程序来打开的方式类型,当该扩展名文件被访问的时候,浏览器会自动使用指定应用程序来打开。多用于指定一些客户端自定义的文件名,以及一些媒体文件打开方式。
服务器会将它们发送的多媒体数据的类型告诉浏览器,而通知手段就是说明该多媒体数据的MIME类型,从而让浏览器知道接收到的信息哪些是MP3文件,哪些是Shockwave文件等等。服务器将MIME标志符放入传送的数据中来告诉浏览器使用哪种插件读取相关文件。

URI:Web上可用的每种资源 - HTML文档、图像、视频片段、程序等 - 由一个通用资源标志符(Uniform Resource Identifier, 简称"URI")进行定位.URL,URN是URI的子集.
From WIKI
URI Synax: <scheme name> : <hierarchical part> [ ? <query> ] [ # <fragment> ]
The scheme name consists of a sequence of characters beginning with a letter and followed by any combination of letters, digits, plus ("+"), period ("."), or hyphen ("-"). Although schemes are case-insensitive, the canonical form is lowercase and documents that specify schemes must do so with lowercase letters. It is followed by a colon (":").

The hierarchical part of the URI is intended to hold identification information hierarchical in nature. If this part begins with a double forward slash ("//"), it is followed by an authority part and a path. If the hierarchical path doesn't begin with ("//") it contains only a path.

  • The authority part holds an optional user-information part, terminated with "@" (e.g. username:password@); a hostname (e.g., domain name or IP address); and an optional port number, preceded by a colon ":".
  • The path part, if present, must begin with a forward slash ("/"). The path is a sequence of segments (conceptually similar to directories, though not necessarily representing them) separated by a forward slash ("/"). Historically, each segment was specified to contain parameters separated from it using a semicolon (";"), though this was rarely used in practice and current specifications allow but no longer specify such semantics.

The query is an optional part, separated by a question mark ("?"), that contains additional identification information that is not hierarchical in nature. The query string syntax is not generically defined, but it is commonly organized as a sequence of <key>=<value> pairs, with the pairs separated by a semicolon[1][2][3] or an ampersand. For example:

Semicolon: key1=value1;key2=value2;key3=value3
Ampersand: key1=value1&key2=value2&key3=value3

The fragment is an optional part separated from the front parts by a hash ("#"). It holds additional identifying information that provides direction to a secondary resource, e.g., a section heading (in an article) identified by the remainder of the URI. When the primary resource is an HTML document, the fragment is often an id attribute of a specific element and web browsers will make sure this element is visible.

[edit] Examples

The following figure displays two example URIs (foo://username:password@example.com:8042/over/there/index.dtb?type=animal&name=narwhal#nose and urn:example:animal:ferret:nose) and their component parts. (The examples are derived from RFC 3986 — STD 66, chapter 3).

  foo://username:password@example.com:8042/over/there/index.dtb?type=animal&name=narwhal#nose
  \_/   \_______________/ \_________/ \__/            \___/ \_/ \______________________/ \__/
   |           |               |       |                |    |            |                |
   |       userinfo         hostname  port              |    |          query          fragment
   |    \________________________________/\_____________|____|/ \__/        \__/
   |                    |                          |    |    |    |          |
scheme              authority                    path   |    |    interpretable as keys
 name   \_______________________________________________|____|/       \____/     \_____/
   |                         |                          |    |          |           |
   |                 hierarchical part                  |    |    interpretable as values
   |                                                    |    |
   |            path               interpretable as filename |
   |   ___________|____________                              |
  / \ /                        \                             |
  urn:example:animal:ferret:nose               interpretable as extension

                path
         _________|________
 scheme /                  \
  name  userinfo  hostname       query
  _|__   ___|__   ____|____   _____|_____
 /    \ /      \ /         \ /           \
 mailto:username@example.com?subject=Topic
目前来看,android中uri scheme有以下三种:
content:// 表明
Android必须从ContentProvider中挑出一个来处理这个URI。
file://表明是本地文件;
http://  或者 rtsp://流媒体文件

Android ContentProvider的Uri由以下三部分组成:

content://

一个标准的URI前缀,它用来指示Android必须从ContentProvider中挑出一个来处理这个URI。

authority

一个唯一的标识符,标示这个URI需要查找的ContentProvider是由哪个组织开发的,一般用跟java包命名规范相似的方式来署名这个 开发组织。如果是Android内置的provider,则这个authority可以省略掉,否则authority是必须的,Google建议使用用 户自定义的继承ContentProvider的类的全名来作为,这个ContentProvider所要处理的URI的authority

path(路径)

ContentProvider根据这个路径信息来判断要返回什么类型的数据,所以这个后缀路径可以自由定义,但是有一些条件限制:

如果一个ContentProvider能查询返回很多种不同类型的数据,URI后缀要设置不同类型的数据所对应不同的URI后缀如内置的Contact ContentProvider就是用来处理名片夹,它可以返回很多种类型的数据:联系人、电话、联系方式等。所以对应这些不同类型的数据就有不同的URI后缀匹配:

content://contacts/people/1

上面是返回一个人员

content://contacts/people/1/phone/3

返回一个电话号码。

而且我们要为不同的数据类型设置不同的MIME类型,第一个MIME类型是:vnd.android.cursor.item/person第二个MIME类型是:vnd.android.cursor.item/phone。

同时ContentProvider不仅可以返回单条数据,也可能以目录的形式返回多条数据。以上面2个URI来说,后缀的结尾都是数字,这表示查询对应某条记录,所以返回的数据是单条的,而如果是查询目录的形式就要去掉后面的数字如:

content://contacts/people/1/phone

content://contacts/people

上面就是目录形式,对应他们的MIME类型也不同,所以我们将单条的MIME类型中的item改为dir,所以MIME类型是:vnd.android.cursor.dir/phone和vnd.android.cursor.dir/person。

A: 标准前缀,表达的意思是使用ContentProvider来处理信息。

B: authority

C: 后缀1,一般用来表示请求需要获取的是哪种类型的数据(确定MIME类型的功能)。

D:定位记录。确定对应类型的数据中的哪条记录。

举些例子,如:

所有联系人的Uri: content://contacts/people

某个联系人的Uri: content://contacts/people/5

所有图片Uri: content://media/external

某个图片的Uri:content://media/external/images/media/4

 

Android系统提供了两个用于操作Uri的工具类,分别为UriMatcher 和ContentUris
 
 
posted @ 2013-02-19 13:46  colin_chan  阅读(447)  评论(0编辑  收藏  举报