生活随笔
收集整理的這篇文章主要介紹了
Android中Uri的使用
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
1.Uri
通用資源標志符(Universal Resource Identifier, 簡稱"URI")。
Uri代表要操作的數(shù)據(jù),Android上可用的每種資源 - 圖像、視頻片段等都可以用Uri來表示。
URI一般由三部分組成:
,URI主要分三個部分:scheme, authority and path。其中authority又分為host和port。格式如下:
scheme://host:port/path
舉個實際的例子:
content://com.example.project:200/folder/subfolder/etc
\---------/? \---------------------------/ \---/ \--------------------------/
scheme???????????????? host?????????????? port??????? path
??????????????? \--------------------------------/
????????????????????????? authority? ?
現(xiàn)在大家應(yīng)該知道data flag中那些屬性的含義了吧,看下data flag
<data android:host="string"
????? android:mimeType="string"
????? android:path="string"
????? android:pathPattern="string"
????? android:pathPrefix="string"
????? android:port="string"
????? android:scheme="string" />
但是我們在程序中一般是不直接用URI來標識CP的,是的,正如我們通常見到的用定義的常量來標識。例如standard CP中的Contacts,我們就用Contacts.People.CONTENT_URI來標識Contacts CP中People這個表。那么要標識某個具體的人怎么辦呢? 這就用到了ContentUris.withAppendedId() 和 Uri.withAppendedPath()。例如我們要表示content://contacts/people/20,那么我們就可以用如下語句:
Uri uri = ContentUris.withAppendedId(People.CONTENT_URI, 20); 或者
Uri uri = Uri.withAppendedPath(People.CONTENT_URI, "20");
訪問資源的命名機制。?
存放資源的主機名。?
資源自身的名稱,由路徑表示。?
Android的Uri由以下三部分組成: "content://"、數(shù)據(jù)的路徑、標示ID(可選)
舉些例子,如:?
所有聯(lián)系人的Uri: content://contacts/people
某個聯(lián)系人的Uri: content://contacts/people/5
所有圖片Uri: content://media/external
某個圖片的Uri:content://media/external/images/media/4
我們很經(jīng)常需要解析Uri,并從Uri中獲取數(shù)據(jù)。
Android系統(tǒng)提供了兩個用于操作Uri的工具類,分別為UriMatcher 和ContentUris 。
雖然這兩類不是非常重要,但是掌握它們的使用,會便于我們的開發(fā)工作。
下面就一起看一下這兩個類的作用。
2.UriMatcher
UriMatcher 類主要用于匹配Uri.
使用方法如下。
首先第一步,初始化:
UriMatcher?matcher?=?new?UriMatcher(UriMatcher.NO_MATCH);?? UriMatcher?matcher?=?new?UriMatcher(UriMatcher.NO_MATCH);?? 第二步注冊需要的Uri:
matcher.addURI("com.yfz.Lesson",?"people",?PEOPLE);??matcher.addURI("com.yfz.Lesson",?"person/#",?PEOPLE_ID);?? matcher.addURI("com.yfz.Lesson",?"people",?PEOPLE);??matcher.addURI("com.yfz.Lesson",?"person/#",?PEOPLE_ID);?? 第三部,與已經(jīng)注冊的Uri進行匹配:
Uri?uri?=?Uri.parse("content://"?+?"com.yfz.Lesson"?+?"/people");??int?match?=?matcher.match(uri);?????????switch?(match)?????????{?????????????case?PEOPLE:?????????????????return?"vnd.Android.cursor.dir/people";?????????????case?PEOPLE_ID:?????????????????return?"vnd.android.cursor.item/people";?????????????default:?????????????????return?null;?????????}?? Uri?uri?=?Uri.parse("content://"?+?"com.yfz.Lesson"?+?"/people");??int?match?=?matcher.match(uri);?????????switch?(match)?????????{?????????????case?PEOPLE:?????????????????return?"vnd.Android.cursor.dir/people";?????????????case?PEOPLE_ID:?????????????????return?"vnd.Android.cursor.item/people";?????????????default:?????????????????return?null;?????????}?? match方法匹配后會返回一個匹配碼Code,即在使用注冊方法addURI時傳入的第三個參數(shù)。?
上述方法會返回"vnd.Android.cursor.dir/person".?
總結(jié):?
--常量 UriMatcher.NO_MATCH 表示不匹配任何路徑的返回碼
--# 號為通配符
--* 號為任意字符?
另外說一下,官方SDK說明中關(guān)于Uri的注冊是這樣寫的:
private?static?final?UriMatcher?sURIMatcher?=?new?UriMatcher();??????static??????{??????????sURIMatcher.addURI("contacts",?"/people",?PEOPLE);??????????sURIMatcher.addURI("contacts",?"/people/#",?PEOPLE_ID);??????????sURIMatcher.addURI("contacts",?"/people/#/phones",?PEOPLE_PHONES);??????????sURIMatcher.addURI("contacts",?"/people/#/phones/#",?PEOPLE_PHONES_ID);??????????sURIMatcher.addURI("contacts",?"/people/#/contact_methods",?PEOPLE_CONTACTMETHODS);??????????sURIMatcher.addURI("contacts",?"/people/#/contact_methods/#",?PEOPLE_CONTACTMETHODS_ID);??????????sURIMatcher.addURI("contacts",?"/deleted_people",?DELETED_PEOPLE);??????????sURIMatcher.addURI("contacts",?"/phones",?PHONES);??????????sURIMatcher.addURI("contacts",?"/phones/filter/*",?PHONES_FILTER);??????????sURIMatcher.addURI("contacts",?"/phones/#",?PHONES_ID);??????????sURIMatcher.addURI("contacts",?"/contact_methods",?CONTACTMETHODS);??????????sURIMatcher.addURI("contacts",?"/contact_methods/#",?CONTACTMETHODS_ID);??????????sURIMatcher.addURI("call_log",?"/calls",?CALLS);??????????sURIMatcher.addURI("call_log",?"/calls/filter/*",?CALLS_FILTER);??????????sURIMatcher.addURI("call_log",?"/calls/#",?CALLS_ID);??????}?? private?static?final?UriMatcher?sURIMatcher?=?new?UriMatcher();??????static??????{??????????sURIMatcher.addURI("contacts",?"/people",?PEOPLE);??????????sURIMatcher.addURI("contacts",?"/people/#",?PEOPLE_ID);??????????sURIMatcher.addURI("contacts",?"/people/#/phones",?PEOPLE_PHONES);??????????sURIMatcher.addURI("contacts",?"/people/#/phones/#",?PEOPLE_PHONES_ID);??????????sURIMatcher.addURI("contacts",?"/people/#/contact_methods",?PEOPLE_CONTACTMETHODS);??????????sURIMatcher.addURI("contacts",?"/people/#/contact_methods/#",?PEOPLE_CONTACTMETHODS_ID);??????????sURIMatcher.addURI("contacts",?"/deleted_people",?DELETED_PEOPLE);??????????sURIMatcher.addURI("contacts",?"/phones",?PHONES);??????????sURIMatcher.addURI("contacts",?"/phones/filter/*",?PHONES_FILTER);??????????sURIMatcher.addURI("contacts",?"/phones/#",?PHONES_ID);??????????sURIMatcher.addURI("contacts",?"/contact_methods",?CONTACTMETHODS);??????????sURIMatcher.addURI("contacts",?"/contact_methods/#",?CONTACTMETHODS_ID);??????????sURIMatcher.addURI("call_log",?"/calls",?CALLS);??????????sURIMatcher.addURI("call_log",?"/calls/filter/*",?CALLS_FILTER);??????????sURIMatcher.addURI("call_log",?"/calls/#",?CALLS_ID);??????}??? 這個說明估計已經(jīng)是Google官方?jīng)]有更新,首先是初始化方法,沒有傳參,那么現(xiàn)在初始化時,實際是必須傳參的。 可以看一下Android2.2的源碼,無參數(shù)的構(gòu)造方法已經(jīng)是private的了。
另外就是addURI這個方法,第二個參數(shù)開始時不需要"/", 否則是無法匹配成功的。
3.ContentUris
ContentUris 類用于獲取Uri路徑后面的ID部分
1)為路徑加上ID: withAppendedId(uri, id)
比如有這樣一個Uri
Uri?uri?=?Uri.parse("content://com.yfz.Lesson/people")?? Uri?uri?=?Uri.parse("content://com.yfz.Lesson/people")?? 通過withAppendedId方法,為該Uri加上ID
Uri?resultUri?=?ContentUris.withAppendedId(uri,?10);?? Uri?resultUri?=?ContentUris.withAppendedId(uri,?10);?? 最后resultUri為: content://com.yfz.Lesson/people/10
2)從路徑中獲取ID: parseId(uri)
Uri?uri?=?Uri.parse("content://com.yfz.Lesson/people/10")??long?personid?=?ContentUris.parseId(uri);?? Uri?uri?=?Uri.parse("content://com.yfz.Lesson/people/10")??long?personid?=?ContentUris.parseId(uri);?? 最后personid 為 :10?
附上實驗的代碼:
package?com.yfz;??import?com.yfz.log.Logger;??import?Android.app.Activity;??import?android.content.ContentUris;??import?android.content.UriMatcher;??import?android.net.Uri;??import?android.os.Bundle;??public?class?Lesson_14?extends?Activity?{????????????????????private?static?final?String?AUTHORITY?=?"com.yfz.Lesson";??????????private?static?final?int?PEOPLE?=?1;??????????private?static?final?int?PEOPLE_ID?=?2;??????????????????????????????private?static?final?UriMatcher?sURIMatcher?=?new?UriMatcher(UriMatcher.NO_MATCH);??????????static??????????{??????????????sURIMatcher.addURI(AUTHORITY,?"people",?PEOPLE);??????????????????????????????????????????sURIMatcher.addURI(AUTHORITY,?"people/#",?PEOPLE_ID);??????????}????????????????????@Override??????????protected?void?onCreate(Bundle?savedInstanceState)?{??????????????super.onCreate(savedInstanceState);??????????????Logger.d("------?Start?Activity?!!!?------");????????????????????????????Uri?uri1?=?Uri.parse("content://"?+?AUTHORITY?+?"/people");??????????????Logger.e("Uri:"?+?uri1);??????????????Logger.d("Match?1"?+?getType(uri1));????????????????????????????Uri?uri2?=?Uri.parse("content://"?+?AUTHORITY?+?"/people"?+?"/2");????????????????????????????Logger.e("Uri:"?+?uri2);??????????????Logger.d("Match?2"?+?getType(uri2));??????????????????????????????????????????Uri?cUri?=?ContentUris.withAppendedId(uri1,?15);??????????????Logger.e("Uri:"?+?cUri);????????????????????????????long?id?=?ContentUris.parseId(cUri);??????????????Logger.d("Uri?ID:?"?+?id);??????????}????????????????????private?String?getType(Uri?uri)?{??????????????int?match?=?sURIMatcher.match(uri);??????????????switch?(match)??????????????{??????????????????case?PEOPLE:??????????????????????return?"vnd.android.cursor.dir/person";??????????????????case?PEOPLE_ID:??????????????????????return?"vnd.android.cursor.item/person";??????????????????default:??????????????????????return?null;??? }??????? } }?
總結(jié)
以上是生活随笔為你收集整理的Android中Uri的使用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。