Adding a New Record
To add a new record, call ContentResolver.insert() with the URI of the type of item to add, and a Map of any values you want to set immediately on the new record. This will return the full URI of the new record, including record number, which you can then use to query and get a Cursor over the new record.
Remember, whenever calling updating methods on the Cursor class, you must call commitUpdates() to send the changes to the database.
To save a file, you can call ContentResolver().openOutputStream() with the URI as shown in the following snippet:
Code:
// Save the name and description in a map. Key is the content provider's
// column name, value is the value to save in that record field.
HashMap<String, Object> values = new HashMap<String, Object>();
values.put(Media.Images.NAME, "road_trip_1");
values.put(Media.Images.DESCRIPTION, "Day 1, trip to Los Angeles");
// Add a new record without the bitmap, but with the values.
// It returns the URI of the new record.
Uri uri = getContentResolver().insert(Media.Images.CONTENT_URI, values);
// Now get a handle to the file for that record, and save the data into it.
// sourceBitmap is a Bitmap object representing the file to save to the database.
OutputStream outStream = getContentResolver.openOutputStream(uri);
sourceBitmap.compress(Bitmap.CompressFormat.JPEG, 50, outStream);
outStream.close();