With this in mind, the MediaStore content provider, which is the main content provider used to distribute image, audio, and video data, utilizes a special convention: the URI used by the query () or managedQuery () method to obtain metadata of the binary data can also be used by the openInputStream () method for data itself. Similarly, the URI used to store metadata in the insert () method of a MediaStore record can also be used by the openOutputStream () method to store binary data. The following code snippet illustrates this Convention:
Java code:
- Import android. provider. MediaStore. Images. Media;
- Import android. content. ContentValues;
- Import java. io. OutputStream;
- // Save the name and description of an image in a ContentValues map.
- ContentValues values = new ContentValues (3 );
- Values. put (Media. DISPLAY_NAME, "road_trip_1 ");
- Values. put (Media. DESCRIPTION, "Day 1, trip to Los Angeles ");
- Values. put (Media. MIME_TYPE, "image/jpeg ");
- // Add a new record without the bitmap, but with the values just set.
- // Insert () returns the URI of the new record.
- Uri uri = getContentResolver (). insert (Media. EXTERNAL_CONTENT_URI, values );
- // Now get a handle to the file for that record, and save the data into it.
- // Here, sourceBitmap is a Bitmap object representing the file to save to the database.
- Try {
- OutputStream outStream = getContentResolver (). openOutputStream (uri );
- SourceBitmap. compress (Bitmap. CompressFormat. JPEG, 50, outStream );
- OutStream. close ();
- } Catch (Exception e ){
- Log. e (TAG, "exception while writing image", e );
- }
Copy code
Batch update record Batch updating records
To update a group of records in batches (for example, change "NY" in all fields to "New York"), you can call ContentResolver by passing the column and value parameters to be changed. update () method.
Delete a record Deleting a record
To delete a single record, you can use the URI parameter of a Specific Row to call the ContentResolver. delete () method.
To delete multiple rows of records, you can call ContentResolver by passing the URI parameter of the record type to be deleted. delete () method (for example, android. provider. contacts. people. CONTENT_URI) and an SQL WHERE statement to define which rows are to be deleted.
Create a Content Provider Creating a Content Provider
To create a content provider, you must:
Create a data storage system. Most content providers use the Android file storage method or SQLite database to store their data, but you can store data in any way you want. Android provides the SQLiteOpenHelper class to help you create a database and manage it using the SQLiteDatabase class.
Extends the ContentProvider class to provide data access interfaces.
Declare this content provider (AndroidManifest. xml) for your application in the manifest file ).
Extended ContentProvider class Extending the ContentProvider class
You can define a ContentProvider subclass to expose your data to other applications that comply with the ContentResolver and Cursor object conventions. Theoretically, this means that six Abstract methods of the ContentProvider class must be implemented:
Query ()
Insert ()
Update ()
Delete ()
GetType ()
OnCreate ()
The query () method must return a Cursor object that can be used to traverse request data. The Cursor itself is an interface, but Android provides some ready-made Cursor objects for you to use. For example, SQLiteCursor can be used to traverse the SQLite database. You can obtain it by calling the query () method of any SQLiteDatabase class. There are also some other cursor implementations-such as MatrixCursor-used to access data that is not stored in the database.
Because the methods of these content providers can be called from various ContentResolver objects of different processes and threads, they must be implemented in thread-safe mode.
To be considerate, when the data is modified, you may also need to call the ContentResolver. policychange () method to notify the listener.
In addition to defining subclass, you should also take other steps to simplify the work of the client and make the class more accessible:
Define a public static final Uri named CONTENT_URI. This is the string of the entire content: URI processed by your content provider. You must define a unique string for it. The best solution is to use the full name of the content provider (fully qualified) Class Name (lower case ). Therefore, for example, a TransportationProvider class can be defined as follows:
Java code:
- Public static final Uri CONTENT_URI = Uri. parse ("content: // com. example. codelab. transporationprovider ");
Copy code