日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

Android中Uri的使用

發布時間:2025/4/5 54 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android中Uri的使用 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1.Uri

通用資源標志符(Universal Resource Identifier, 簡稱"URI")。

Uri代表要操作的數據,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? ?

現在大家應該知道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://"、數據的路徑、標示ID(可選)

舉些例子,如:?

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

某個聯系人的Uri: content://contacts/people/5

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

某個圖片的Uri:content://media/external/images/media/4

我們很經常需要解析Uri,并從Uri中獲取數據。

Android系統提供了兩個用于操作Uri的工具類,分別為UriMatcher 和ContentUris

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

下面就一起看一下這兩個類的作用。

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);??
  • 第三部,與已經注冊的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時傳入的第三個參數。?

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

    總結:?

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

    --# 號為通配符

    --* 號為任意字符?

    另外說一下,官方SDK說明中關于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);??
  • ????}???
  • 這個說明估計已經是Google官方沒有更新,首先是初始化方法,沒有傳參,那么現在初始化時,實際是必須傳參的。 可以看一下Android2.2的源碼,無參數的構造方法已經是private的了。

    另外就是addURI這個方法,第二個參數開始時不需要"/", 否則是無法匹配成功的。

    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;??
  • ??????????
  • ????????//NO_MATCH表示不匹配任何路徑的返回碼??
  • ????????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??
  • ????????????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;??? }??????? } }?

  • 總結

    以上是生活随笔為你收集整理的Android中Uri的使用的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。