import android.provider.Contacts.People; import android.content.ContentResolver; import android.content.ContentValues; ContentValues values = new ContentValues(); // Add Abraham Lincoln to contacts and make him a favorite. values.put(People.NAME, "Abraham Lincoln"); // 1 = the new contact is added to favorites // 0 = the new contact is not added to favorites values.put(People.STARRED, 1); Uri uri = getContentResolver().insert(People.CONTENT_URI, values);添加新的值
Uri phoneUri = null; Uri emailUri = null; // Add a phone number for Abraham Lincoln. Begin with the URI for // the new record just returned by insert(); it ends with the _ID // of the new record, so we don't have to add the ID ourselves. // Then append the designation for the phone table to this URI, // and use the resulting URI to insert the phone number. phoneUri = Uri.withAppendedPath(uri, People.Phones.CONTENT_DIRECTORY); values.clear(); values.put(People.Phones.TYPE, People.Phones.TYPE_MOBILE); values.put(People.Phones.NUMBER, "1233214567"); getContentResolver().insert(phoneUri, values); // Now add an email address in the same way. emailUri = Uri.withAppendedPath(uri, People.ContactMethods.CONTENT_DIRECTORY); values.clear(); // ContactMethods.KIND is used to distinguish different kinds of // contact methods, such as email, IM, etc. values.put(People.ContactMethods.KIND, Contacts.KIND_EMAIL); values.put(People.ContactMethods.DATA, "[email protected]"); values.put(People.ContactMethods.TYPE, People.ContactMethods.TYPE_HOME); getContentResolver().insert(emailUri, values);
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); }批量更新数据
public static final Uri CONTENT_URI = Uri.parse("content://com.example.codelab.transportationprovider");如果这个provider包含子表,那么同样需要为每一个子表定义CONTENT_URI常量,这些子表拥有相同的权限(也就是content provider的标示符),并且这些子表仅仅由他们的路径来区分,如下面示例:
content://com.example.codelab.transportationprovider/train content://com.example.codelab.transportationprovider/air/domestic content://com.example.codelab.transportationprovider/air/international对于content:URI的整体介绍,详看本文最后的Content URI总结部分。
<provider android:name="com.example.autos.AutoInfoProvider" android:authorities="com.example.autos.autoinfoprovider" . . . /> </provider>注意authorities属性忽略了content:URI的路径。例如:如果AutoINfoProvider对不同类型的autos或者不同的制造商拥有子表,
content://com.example.autos.autoinfoprovider/honda content://com.example.autos.autoinfoprovider/gm/compact content://com.example.autos.autoinfoprovider/gm/suv这些路径不应该在AndroidManifest文件中声明。因为权限是对provider而言的,而不是对于路径;你的provider应该可以以自己选择的方式来解释URI的路径信息部分。
<provider android:name=".TransportationProvider" android:authorities="com.example.transportationprovider" . . . >C.这部分是content provider用来决定是哪些类型的数据被请求。这个可以为空,也可以有几个段那么长。如果一个content provider仅仅暴露一种类型的数据(例如:仅仅是trains类型),那么他就可以为空。如果一个content provider暴露几种类型的数据,包括子类型,那么它就有可能有几段那么长——例如:“land/bus”、“labd/train”、"sea/ship"和“sea/submarine”这四种可能的类型。
D.被查询的特殊记录的ID。如果有,那么这个就是被请求记录的_ID值。如果请求不是对单个记录,那么这个段和斜杠都会被忽略:
content://com.example.transportationprovider/trains