Android ContentProvider

  • Replies:0
  • OpenNot stickiedUnanswered

Apr 8, 2013 8:09:42 AM via Website

Content Providers are a generic interface mechanism that lets you share data between applications. By abstracting away the underlying data source, Content Providers let you decouple your application layer from the data layer, making your applications data-source agnostic.
Content Providers feature full permission control and are accessed using a simple URI model. Shared content can be queried for results as well as supporting write access. As a result, any application with the appropriate permissions can add, remove, and update data from any other applications — including some native Android databases.
9android developer has guided beginner at http://www.9android.net/content-provider/

Using Content Providers

Each application Context has a single ContentResolver, accessible using the getContentResolver
method, as shown in the following code snippet:

1ContentResolver cr = getContentResolver();

Content Resolver includes several methods to transact and query Content Providers. You specify the provider to interact using a URI.
A Content Provider’s URI is defined by its authorityas defined in its application manifest node. An
authority URI is an arbitrary string, so most providers expose a CONTENT_URI property that includes its
authority.
Content Providers usually expose two forms of URI, one for requests against all the data and
another that specifies only a single row. The form for the latter appends / to the standard CONTENT_URI.
2.1. Introducing ContentResolver

As in databases, query results are returned as Cursors over a result set. You can extract values from the cursor using the techniques described previously within the database section on “Extracting Results from a Cursor.”
Content Provider queries take a very similar form to database queries. Using the querymethod on the ContentResolverobject, pass in:
+ The URI of the content provider data you want to query .
+ A projection that represents the columns you want to include in the result set.
+ A whereclause that defi nes the rows to be returned. You can include ?wild cards that will be replaced by the values stored in the selection argument parameter.
+ An array of selection argument strings that will replace the ?’s in the whereclause
+ A string that describes the order of the returned rows
The following skeleton code demonstrates the use of a Content Resolver to apply a query to a Content Provider:
1// Return all rows
2 Cursor allRows = getContentResolver().query(MyProvider.CONTENT_URI,
3 null, null, null, null);
4 // Return all columns for rows where column 3 equals a set value
5 // and the rows are ordered by column 5.
6 String where = KEY_COL3 + "=" + requiredValue;
7 String order = KEY_COL5;
8 Cursor someRows = getContentResolver().query(MyProvider.CONTENT_URI, null, where, null, order);

2.2. Querying for Content

Adding, Updating, and Deleting Content
To perform transactions on Content Providers, use the delete, update, and insert methods on the
ContentResolver object.
+ Inserts
The Content Resolver offers two methods for inserting new records into your Content Provider insert and bulkInsert. Both methods accept the URI of the item type you’re adding; where the former takes a single new ContentValues object, the latter takes an array.
The simple insert method will return a URI to the newly added record, while bulkInsert returns the
number of successfully added items.
The following code snippet shows how to use the insert and bulkInsert methods:

1// Create a new row of values to insert.
2 ContentValues newValues = new ContentValues();
3 // Assign values for each row.
4 newValues.put(COLUMN_NAME, newValue);
5 [ ... Repeat for each column ... ]
6 Uri myRowUri = getContentResolver().insert(MyProvider.CONTENT_URI,newValues);
7 // Create a new row of values to insert.
8 ContentValues[] valueArray = new ContentValues[5];
9 // TODO: Create an array of new rows
10 int count = getContentResolver().bulkInsert(MyProvider.CONTENT_URI, valueArray);

+ Deletes
To delete a single record using the Content Resolver, call delete, passing in the URI of the row you
want to remove. Alternatively, you can specify a WHERE clause to remove multiple rows. Both techniques
are shown in the following snippet:
1// Remove a specific row.
2 getContentResolver().delete(myRowUri, null, null);
3 // Remove the first five rows.
4 String where = "_id < 5";
5 getContentResolver().delete(MyProvider.CONTENT_URI, where, null);

+ Updates
Updates to a Content Provider are handled using the update method on a Content Resolver. The update method takes the URI of the target Content Provider, a ContentValues object that maps column names to updated values, and a WHERE clause that specifies which rows to update.
When executed, every matching row in the WHERE clause will be updated using the values in the ContentValues passed in and will return the number of successful updates.

1// Create a new row of values to insert.
2 ContentValues newValues = new ContentValues();
3 // Create a replacement map, specifying which columns you want to
4 // update, and what values to assign to each of them.
5 newValues.put(COLUMN_NAME, newValue);
6 // Apply to the first 5 rows.
7 String where = "_id < 5";
8 getContentResolver().update(MyProvider.CONTENT_URI, newValues, where,null);

2.3. Accessing Files in Content Providers

Content Providers represent files as fully qualified URIs rather than raw file data. To insert a file into a Content Provider, or access a saved file, use the ContentResolvers openOutputStream or openInputStream methods, respectively. The process for storing a file is shown in the following code snippet:

1// Insert a new row into your provider, returning its unique URI.
2 Uri uri = getContentResolver().insert(MyProvider.CONTENT_URI,newValues);
3 try {
4 // Open an output stream using the new row’s URI.
5 OutputStream outStream = getContentResolver().openOutputStream(uri);
6 // Compress your bitmap and save it into your provider.
7 sourceBitmap.compress(Bitmap.CompressFormat.JPEG, 50, outStream);
8 }
9 catch (FileNotFoundException e) {}

— modified on Apr 15, 2013 7:03:07 AM