Content Provider in Android — Basics
Content Provider in Android — In our series of Android articles, Previously we discussed about Activity , Services and Fragment. Here we are going to discuss about content providers and to cover the basic concepts you need to make use of existing ones or to write content providers on your own.
It gets better,
What are Content Providers?
A Content Provider component supplies data from one application to others on request. Such requests are handled by the methods of the ContentResolver class. A content provider can use different ways to store its data and the data can be stored in files, in a database or even over a network.
Content Providers support the four basic operations, normally called CRUD-operations. CRUD is the acronym for create, read, update and delete. With content providers those objects simply represent data as most often a record of a database — but they could also be a photo on your SD-card or a video on the web.
Content URIs
The most important concept to understand when dealing with content providers is the content URI. Whenever you want to access data from a content provider you have to specify a URI. URIs for content providers look like this:content://authority/optionalPath/optionalId
Here, detailed explanation about Content URIs
- The scheme for content providers is always “content”. The colon and double-slash “://” are a fixed part
- This specifies the name of the content provider, for example browser , contacts etc. Authorities have to be unique for every content provider. Android documentation recommends to use the fully qualified class name of your ContentProvider-subclass.
- The optional path, is used to distinguish the kinds of data your content provider offers.For example, distinguishes between audio files, video files and images using different paths for each of these types of media. This way a content provider can support different types of data that are nevertheless related. For totally unrelated data though you should use different content providers — and thus different authorities.
- The last element is the optional id, which — if present — must be numeric. The id is used whenever you want to access a single record (e.g. a specific video file).
- directory-based URIs
- id-based URIs.
If no id is specified a URI is automatically a directory-based URI.
If You use directory-based URIs to access multiple elements of the same type (e.g. all songs of a band). All CRUD-operations are possible with directory-based URIs.
You use id-based URIs if you want to access a specific element. You cannot create objects using an id-based URI — but reading, updating and deleting is possible.
Check these out : Complete Professional Android Training Topics
Content Types
A content type consist of a two types such as media type and a subtypedivided by a slash. A typical example is “image/png”. The media type “image” describes the content as an image file which is further specified to be of the Portable Network Graphic variety by the subtype “png”.
And guess what?
vnd.android.cursor.item — Used for single records
vnd.android.cursor.dir — Used for multiple records
The subtype on the other hand is used for content provider specific details and should differ for all types your content provider supports. The naming convention for subtypes is vnd.companyname.contenttype. Most content providers support multiple subtypes.
Which standard Content Providers are available?
A number of content providers are part of Android’s API. All these standard providers are defined in the package android.provider. The below lists the standard providers and what they are used for
- CalendarContract SDK 14 — Manages the calendars on the user’s device.
- Browser SDK 1 — Manages your web-searches, bookmarks and browsing-history.
- CallLog SDK 1 — Keeps track of your call history.
- MediaStore SDK 1 — The content provider responsible for all your media files like music, video and pictures.
- Settings SDK 1 — Manages all global settings of your device.
- UserDictionary SDK 3 — Keeps track of words you add to the default dictionary.
Conclusion:
In this part of this mini series you have learned all about the two types of URIs for content providers as well as about the two media types of content types.
News Flash: Google’s Latest update was named as Android Oreo
Check out : Android O — Cool and New Features in the latest Google’s Update
Comments
Post a Comment