Detailed explanation of android Uri

First look at the renderings:


 

 

1.Uri

Universal Resource Identifier ( Universal Resource Identifier, "URI" for short ).

Uri represents the data to be manipulated, and every resource available on Android - images, video clips, etc. can be represented by Uri .

A URI generally consists of three parts:

A naming mechanism for accessing resources. 

The hostname where the resource is stored. 

The name of the resource itself, represented by a path. 

Android 's Uri consists of the following three parts: "content://" , the path of the data, and the mark ID ( optional )

Some examples like: 

Uri for all contacts : content://contacts/people

Uri of a contact : content://contacts/people/5

All images Uri: content://media/external

Uri of an image : content://media/external/images/media/4

We often need to parse Uri and get data from Uri .

The Android system provides twotool classesUriUriMatcherandContentUris .

Although these two categories are not very important, mastering their use will facilitate our development work.

2.UriMatcher

The UriMatcher class is mainly used to match Uri.

The method of use is as follows.

//First step, initialization:

UriMatcher matcher = new UriMatcher (UriMatcher.NO_MATCH);  

UriMatcher matcher = new UriMatcher (UriMatcher.NO_MATCH);  

//The second step is to register the required 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);  

//The third step is to match the registered 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;  

       }  

 

 

 

 

After the match method matches, it will return a match code Code , which is the third parameter passed in when using the registration method addURI . 

The above method will return " vnd.Android.cursor.dir /person". 

3.ContentUris

The ContentUris class is used to get the ID part behind the Uri path

3.1 Add an ID to the path

For example, there is such a Uri

Uri uri = Uri.parse("content://com.yfz.Lesson/people")  

Uri uri = Uri.parse("content://com.yfz.Lesson/people")  

Add an ID to the Uri through the withAppendedId method

Uri resultUri = ContentUris.withAppendedId (uri, 10);  

Uri resultUri = ContentUris.withAppendedId (uri, 10);  

The final resultUri is: content://com.yfz.Lesson/people/10

 

3.2 Get ID from path

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);  

 The last personid is : 10 


 Demo download
Finally, the above examples are all from Android worry-free, please go to the application treasure or pea pod to download: http://android.myapp.com/myapp/detail.htm?apkName=com.shandong.mm.androidstudy , source code example Documentation is exhausted.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326612599&siteId=291194637