Android Content Provider And ContentResolver Example

Android content provider is mainly used for data sharing between different applications. It provides a complete set of mechanisms to allow one program to access data in another program, and also to ensure the security of the data being accessed.

read data from android content provider

1. Content Provider.

By using content provider, users can choose to share only some part of the data in one application, thus can avoid privacy data leak in the program. At present, the use of content provider is the standard way for Android to share data across applications.

1.1 Content URI.

Content URI is a unique resource identifier that content provider app provides for client app to access it’s shared data. The content URI format is like below.


  1. schema : This is the content URI protocol, it’s value is content in general.
  2. authority : This part is used to distinguish different content provider app to avoid conflict, so generally it is the app package name.
  3. path : This part is used to distinguish different shared data in one android content provider app. Different path will return different data.

android content provider uri format

1. ContentResolver.

Each android application can be a content provider. For example, android phone contacts, short message system and android media library.

To get data from a content provider, you need to use a ContentResolver instance in your app. Generally the ContentResolver instance can be obtained by Activity‘s getContentResolver() method.

ContentResolver contentResolver = getContentResolver();

Then you can invoke ContentResolver‘s method to insert, delete, update and query data that another content provider shared. This is something like SQLite  database operation.

2. ContentResolver Methods.

Before process data operation, you should first get the content provider URI instance using below method.

Uri contentUri = Uri.parse("content://com.dev2qa.example.provider/userinfo");

2.1 Insert Data To Content Provider.

insert(Uri providerUri, ContentValues values)

Uri contentUri = Uri.parse("content://....");

ContentValues contentValues = new ContentValues();

contentValues.put("column1", value1);

contentValues.put("column2", value2);

getContentResolver().insert(contentUri, contentValues);

2.2 Update Content Provider Data.

update(Uri providerUri, ContentValues values, String whereClause, String conditionValueArr[])

Uri contentUri = Uri.parse("content://....");

ContentValues contentValues = new ContentValues();

contentValues.put("userName", userName);

contentValues.put("password", passwrod);

String whereClause = "id = ?";

String placeHolderValueArr[] = {"1"}

getContentResolver().update(contentUri, contentValues, whereClause , placeHolderValueArr);

2.3 Delete Content Provider Data.

delete(Uri providerUri, String whereClause, String conditionValueArr[])

Uri contentUri = Uri.parse("content://....");

String whereClause = "id = ?";

String placeHolderValueArr[] = {"1"}

getContentResolver().delete(contentUri, whereClause , placeHolderValueArr);

2.4 Query Content Provider Data.

query(Uri uri, String columnArray[], String whereClause, String wherePlaceHolderValue[], String orderByClause)

The query method return a android.database.Cursor object, if it is not null, then use it’s moveToFirst() method to move to the first row.

Then loop in the cursor to get each row use it’s moveToNext() method.

Uri contentUri = Uri.parse("content://....");

Cursor cursor = getContentResolver().query(contentUri , null, null, null, null);


    // Loop in the cursor to get each row.
        // Get column 1 value.
        int column1Index= cursor.getColumnIndex("column1");
        String column1Value= cursor.getString(column1Index);

        // Get column 2 value.
        int column2Index= cursor.getColumnIndex("column2");
        String column2Value= cursor.getString(column2Index);
0 0 votes
Article Rating
Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Oldest Most Voted
Inline Feedbacks
View all comments

This is the clearest explanation I found about content provider and content resolver. The picture was really helpful.

Reply to  Archie

Superb >>Thanks…

Would love your thoughts, please comment.x