57

What is the difference between ContentProviders and ContentResolver? I do not want for the SQLite database. I am developing an application for media.

BC2
  • 892
  • 1
  • 7
  • 23
Akshay Mukadam
  • 2,388
  • 1
  • 27
  • 40
  • 6
    the same as between "to provide" and "to resolve" ... short: ContentProvider implementation provides some data for some authority ... ContentResolver resolve which provider(based on authority) should be used and ask this provider for data ... – Selvin Sep 18 '13 at 14:27
  • @Selvin F I want to make a program for playlist than I think I must have to create the content provider.Please tell me how to create our own ContentProviders – Akshay Mukadam Sep 18 '13 at 14:50
  • https://queception.com/question.php?question=107 – Stack Overflow Aug 06 '19 at 06:22
  • 1
    In case any of you have some networking basics, ContentResolver is analogous to domain name resolution, i.e. mapping stackoverflow.com to 151.101.129.69.The role of ContentResolver pretty much ends there. ContentProvider serves the contents for a specific request like providing the HTML for this question "stackoverflow.com/questions/18874801". It is really just an abstraction layer for querying a database, ignoring the underlying implementation – Aswath Jun 02 '20 at 08:54

4 Answers4

69

I found some explanation here. In summary

Content Resolver resolves a URI to a specific Content provider.

Content Provider provides an interface to query content.

The way to query a content provider is contentResolverInstance.query(URI,.....)

Vasily Kabunov
  • 6,511
  • 13
  • 49
  • 53
user1700184
  • 1,611
  • 2
  • 16
  • 23
  • 1
    > if i want to create my playlist than whether I have to use the Contentprovider?? – Akshay Mukadam Sep 18 '13 at 14:47
  • 1
    Yes. Content Provider exposes an application's content to other applications. You can also use it to retrieve data from a web server. – user1700184 Sep 18 '13 at 16:13
  • It is good explanation! But if the writer had explained "ContentProvider" first then it would had given more sense and sequence + more understanding! Anyhow! It clears the idea for me :) – Faizan Mubasher Jan 19 '14 at 14:15
  • Just to add. ContentResolver particularly needed when you access "other's" content providers to have a secure access. If you have your own content provider you don't need to use it. – stdout Jun 27 '16 at 12:07
53

ContentProviders are used to abstract the database from other parts and acts as an interface between your database and UI/other classes. You must create your own ContentProvider to share your app data with other apps.

ContentResolver is used to select the right ContentProvider based on the ContentUris. A ContentUri may look like

content://com.android.contacts/contacts/3

  • content:// is called scheme and indicates that it is a ContentUri.
  • com.android.contacts is called Content authority and ContentResolver uses it to resolve to a unique provider (in this case, ContactProvider).
  • contacts is the path that identify some subset of the provider's data (for example, Table name).
  • 3 is the id used to uniquely identify a row within the subset of data.

enter image description here

NOTE : Your own app can also use this route to handle its data.

See Content Providers in Android for more detail

Siva Prakash
  • 4,626
  • 34
  • 26
23

Two layered Abstraction :

ContentResolver --> ContentProvider -->SQLiteDatabase

The main difference is this as mentioned in other answers.

ContentProvider exposes private data of your application to external application
while
ContentResolver provides the right ContentProvider among all the ContentProviders using a URI.

Deeper Understanding (of two-layered abstraction)

Let's take a detour.
We all know that when we create an SQLite database then the database remains private to your application which means, you just can not share your app data with any other external application.

How data is shared then?

ContentProvider and ContentResolver are part of android.content package. These two classes work together to provide robust, secure data sharing model among applications.
ContentProvider exposes data stored in the SQLite database to other application without telling them the underlying implementation of your database.
So it abstracts the SQliteDatabase. But wait there is a catch !!!
The external application can not directly access ContentProvider. For that, you need to first interact with another class called ContentResolver Think ContentResolver as a ContentProvider finder. There is only one instance of it and all the ContentProviders of your Device are registered with a simple Namespace URI. If you want to reach a particular ContentProvider you just need to know its URI. Pass it to ContentResolver and it will find the Provider using the URI.
Now lets have a look at the most important method getContentResolver().query(URI,String[] proj.....)

What happens when getContentResolver().query(URI,String[] proj.....) gets called

query() method belongs to ContentResolver class however it invokes the abstract query() method of resolved ContentProvider and returns Cursor object.
In this way, the External application gets exposed to the private database via two abstraction layers.

Just to add more points
You cannot create your own ContentResolver class but you can always create your own ContentProvider class

Hope you have a better understanding
You can also see some sample code here for creating SQLitedatabase, ContentProvider etc, But it's not well documented.

Rohit Singh
  • 16,950
  • 7
  • 90
  • 88
6

In 2021 :D

Content Resolver : For Data Request

Content Provider : For Data Response