|
Cyril Sermon (@admin) |
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 Content Resolvers openOutputStream or openInputStream methods, respectively. The process for storing a file is shown in the following code snippet:
Insert a new row into your provider, returning its unique URI.
Uri uri = getContentResolver().insert(MyProvider.CONTENT_URI,
newValues);
try {
// Open an output stream using the new row’s URI.
OutputStream outStream = getContentResolver().openOutputStream(uri);
Compress your bitmap and save it into your provider.
sourceBitmap.compress(Bitmap.CompressFormat.JPEG, 50, outStream);
}
catch (FileNotFoundException e) { }
Android exposes many Content Providers that supply access to the native databases.
You can use each of these Content Providers natively using the techniques described previously. Alter-natively, the android.provider class includes convenience classes that simplify access to many of the most useful providers, including:
❑Browser Use the browser Content Provider to read or modify bookmarks, browser history, or web searches.
❑CallLog View or update the call history including both incoming and outgoing calls together with missed calls and call details, like caller ID and call durations.
❑Contacts Use the Contacts provider to retrieve, modify, or store your contacts’ details.
❑MediaStore The Media Store provides centralized, managed access to the multimedia on your device, including audio, video, and images. You can store your own multimedia within the Media Store and make it globally available.
❑Settings You can access the device’s preferences using the Settings provider. Using it, you can view and modify Bluetooth settings, ring tones, and other device preferences.
You should use these native Content Providers wherever possible to ensure that your application inte-grates seamlessly with other native and third-party applications.
While a detailed description of how to use each of these helpers is beyond the scope of this chapter, the following sections describe how to use some of the more useful and powerful native Content Providers.