日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 运维知识 > Android >内容正文

Android

Android中Uri的使用

發(fā)布時(shí)間:2025/4/5 Android 45 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android中Uri的使用 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

1.Uri

通用資源標(biāo)志符(Universal Resource Identifier, 簡(jiǎn)稱"URI")。

Uri代表要操作的數(shù)據(jù),Android上可用的每種資源 - 圖像、視頻片段等都可以用Uri來(lái)表示。

URI一般由三部分組成:

,URI主要分三個(gè)部分:scheme, authority and path。其中authority又分為host和port。格式如下:

scheme://host:port/path
舉個(gè)實(shí)際的例子:
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" />
但是我們?cè)诔绦蛑幸话闶遣恢苯佑肬RI來(lái)標(biāo)識(shí)CP的,是的,正如我們通常見(jiàn)到的用定義的常量來(lái)標(biāo)識(shí)。例如standard CP中的Contacts,我們就用Contacts.People.CONTENT_URI來(lái)標(biāo)識(shí)Contacts CP中People這個(gè)表。那么要標(biāo)識(shí)某個(gè)具體的人怎么辦呢? 這就用到了ContentUris.withAppendedId() 和 Uri.withAppendedPath()。例如我們要表示content://contacts/people/20,那么我們就可以用如下語(yǔ)句:
Uri uri = ContentUris.withAppendedId(People.CONTENT_URI, 20); 或者
Uri uri = Uri.withAppendedPath(People.CONTENT_URI, "20");


訪問(wèn)資源的命名機(jī)制。?

存放資源的主機(jī)名。?

資源自身的名稱,由路徑表示。?

Android的Uri由以下三部分組成: "content://"、數(shù)據(jù)的路徑、標(biāo)示ID(可選)

舉些例子,如:?

所有聯(lián)系人的Uri: content://contacts/people

某個(gè)聯(lián)系人的Uri: content://contacts/people/5

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

某個(gè)圖片的Uri:content://media/external/images/media/4

我們很經(jīng)常需要解析Uri,并從Uri中獲取數(shù)據(jù)。

Android系統(tǒng)提供了兩個(gè)用于操作Uri的工具類,分別為UriMatcher 和ContentUris

雖然這兩類不是非常重要,但是掌握它們的使用,會(huì)便于我們的開發(fā)工作。

下面就一起看一下這兩個(gè)類的作用。

2.UriMatcher

UriMatcher 類主要用于匹配Uri.

使用方法如下。

首先第一步,初始化:

  • UriMatcher?matcher?=?new?UriMatcher(UriMatcher.NO_MATCH);??
  • UriMatcher?matcher?=?new?UriMatcher(UriMatcher.NO_MATCH);??
  • 第二步注冊(cè)需要的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)注冊(cè)的Uri進(jìn)行匹配:

  • 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方法匹配后會(huì)返回一個(gè)匹配碼Code,即在使用注冊(cè)方法addURI時(shí)傳入的第三個(gè)參數(shù)。?

    上述方法會(huì)返回"vnd.Android.cursor.dir/person".?

    總結(jié):?

    --常量 UriMatcher.NO_MATCH 表示不匹配任何路徑的返回碼

    --# 號(hào)為通配符

    --* 號(hào)為任意字符?

    另外說(shuō)一下,官方SDK說(shuō)明中關(guān)于Uri的注冊(cè)是這樣寫的:

  • 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);??
  • ????}???
  • 這個(gè)說(shuō)明估計(jì)已經(jīng)是Google官方?jīng)]有更新,首先是初始化方法,沒(méi)有傳參,那么現(xiàn)在初始化時(shí),實(shí)際是必須傳參的。 可以看一下Android2.2的源碼,無(wú)參數(shù)的構(gòu)造方法已經(jīng)是private的了。

    另外就是addURI這個(gè)方法,第二個(gè)參數(shù)開始時(shí)不需要"/", 否則是無(wú)法匹配成功的。

    3.ContentUris

    ContentUris 類用于獲取Uri路徑后面的ID部分

    1)為路徑加上ID: withAppendedId(uri, id)

    比如有這樣一個(gè)Uri

  • Uri?uri?=?Uri.parse("content://com.yfz.Lesson/people")??
  • Uri?uri?=?Uri.parse("content://com.yfz.Lesson/people")??
  • 通過(guò)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?

    附上實(shí)驗(yàn)的代碼:

  • 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;??
  • ??????????
  • ????????//NO_MATCH表示不匹配任何路徑的返回碼??
  • ????????private?static?final?UriMatcher?sURIMatcher?=?new?UriMatcher(UriMatcher.NO_MATCH);??
  • ????????static??
  • ????????{??
  • ????????????sURIMatcher.addURI(AUTHORITY,?"people",?PEOPLE);??
  • ??????????????
  • ????????????//這里的#代表匹配任意數(shù)字,另外還可以用*來(lái)匹配任意文本??
  • ????????????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??
  • ????????????Uri?cUri?=?ContentUris.withAppendedId(uri1,?15);??
  • ????????????Logger.e("Uri:"?+?cUri);??
  • ????????????//獲取ID??
  • ????????????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的使用的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

    如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。