Android Content Providers (3)

Source: Internet
Author: User

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:

  1. Import android. provider. MediaStore. Images. Media;
  2. Import android. content. ContentValues;
  3. Import java. io. OutputStream;
  4. // Save the name and description of an image in a ContentValues map.
  5. ContentValues values = new ContentValues (3 );
  6. Values. put (Media. DISPLAY_NAME, "road_trip_1 ");
  7. Values. put (Media. DESCRIPTION, "Day 1, trip to Los Angeles ");
  8. Values. put (Media. MIME_TYPE, "image/jpeg ");
  9. // Add a new record without the bitmap, but with the values just set.
  10. // Insert () returns the URI of the new record.
  11. Uri uri = getContentResolver (). insert (Media. EXTERNAL_CONTENT_URI, values );
  12. // Now get a handle to the file for that record, and save the data into it.
  13. // Here, sourceBitmap is a Bitmap object representing the file to save to the database.
  14. Try {
  15. OutputStream outStream = getContentResolver (). openOutputStream (uri );
  16. SourceBitmap. compress (Bitmap. CompressFormat. JPEG, 50, outStream );
  17. OutStream. close ();
  18. } Catch (Exception e ){
  19. Log. e (TAG, "exception while writing image", e );
  20. }

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:

  1. Public static final Uri CONTENT_URI = Uri. parse ("content: // com. example. codelab. transporationprovider ");

Copy code

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.