Submitted by Dolan Kundu as a part of the First Android Tutorial Contest.[/align]
when we are making the data as public, we can create/call the Content Provider. It is an object that can store and retrieve the data accessible by all applications. In this way we can share the data through out the Packages. For that, there is no common storage area tha all Packages can store. We can use the Content Provider for all common Data Types. such as audio, Video, Images, PCM, etc.
Creation of a Content Provider:
There are the steps to create the Content Provider. The steps are as follows:
1) Create a class that extends ContentProvider.
2) Define a public static final Uri named CONTENT_URI. This String represents fwhole "content://"URI which the Content provider handles.The String must be Unique.As for Example;
public static final Uri CONTENT_URI = Uri.parse("content://com.ibt.android.devprovider");
3) We have to create the System for storing data. Most content providers store their data using Android's file storage methods or SQLite databases..But if we want , we can store the data as any way, so long as we are following the calling and return value.
4) Define the column names that we will return to the clients. If we are using DatabaseContentProvider or SQLiteOpenHelper classes, these column names are typically identical to SQL database column. In any cases , We hve to incude integer column named_id to define the specific record number. If using SQLite database, this be INTEGER PRIMARY KEY AUTOINCREMENT. The AUTOINCREMENT descriptor is optional. SQLite autoincrements an ID counter field to the next number above the largest existing number in the table. Android provides SQLiteOpenHelper class to create and manage versions of database.
5) If we expose byte data, such as a bitmap file, the field that stores this data should actually be a string field with a content:// URI for that specific file. This is the field that clients will call to retrieve this data. The content provider for that content type (it can be the same content provider or another content provider — for example, if you're storing a photo you would use the media content provider) should implement a field named _data for that record. The _data field lists the exact file path on the device for that file. This field is not intended to be read by the client, but by the ContentResolver. The client will call ContentResolver.openOutputStream() on the user-facing field holding the URI for the item (for example, the column named photo might have a value content://media/images/4453). The ContentResolver will request the _data field for that record, and because it has higher permissions than a client, it should be able to access that file directly and return a read wrapper for that file to the client.
6) Declare public static Strings that clients can use to specify which columns to return, or to specify field values from the cursor. Here audio or bitmap fields, are typically returned as string path values.
7) Return a Cursor object over a recordset in reply to a query. It seems implementing the query(), update(), insert(), and delete() methods.We can call ContentResolver.notifyChange() to notify listeners about updated information.
8) Add a <provider> tag to AndroidManifest.xml, and use its authorities attribute to define the authority part of the content type it should handle. For example, if your content type is content://com.example.autos/auto to request a list of all autos, then authorities would be com.example.autos.
9) If we handle a new data type, we must define a new MIME type to return for your implementation of android.ContentProvider.getType(url). This type corresponds to the content:// URI submitted to getType(), which will be one of the content types handled by the provider. The MIME type for each content type has two forms: one for a specific record, and one for multiple records. Use the Uri methods to help determine what is being requested. Here is the general format for each:
- vnd.android.cursor.item/vnd.yourcompanyname.contenttype for a single row.
for multiple rows. For example, a request for all train records using
For an example of a private content provider implementation, see the NodePadProvider class in the notepad sample application that ships with the SDK.
Here is a recap of the important parts of a content URI:
Elements of a content URI:
- Standard required prefix. Never modified.
- Authority part. For third-party applications, this should be a fully-qualified class to ensure uniqueness. This corresponds to the value in the <provider> element's authorities attribute: <provider class="TransportationProvider" authorities="com.example.transportationprovider" />
- The path that the content provider uses to determine what kind of data is being requested. This can be zero or more segments: if the content provider exposes only one type of data (only trains, for example), this can be absent. If it provides several types, including subtypes, this can be several elements long: e.g., "land/bus, land/train, sea/ship, and sea/submarine" to give four possibilities.
- A specific record being requested, if any. This is the _id value of a specific record being requested. If all records of a specific type are being requested, omit this and the trailing slash: content://com.example.transportationprovider/trains