android向联系人中添加头像以及获得电话记录

ContentResolver cr = getContentResolver();
Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null, "DISPLAY_NAME = '" + "gm" + "'", null,
				null);
if (cursor.moveToFirst()) {
//获得联系人的id			
String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));

//修改联系人的头像
Bitmap sourceBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.img2);
final ByteArrayOutputStream os = new ByteArrayOutputStream();
// 将Bitmap压缩成PNG编码,质量为100%存储
sourceBitmap.compress(Bitmap.CompressFormat.PNG, 100, os);
byte[] avatar =os.toByteArray();
ContentValues values = new ContentValues();
values.put(Data.RAW_CONTACT_ID, contactId);
values.put(Data.MIMETYPE, Photo.CONTENT_ITEM_TYPE);
values.put(Photo.PHOTO, avatar);
getContentResolver().update(Data.CONTENT_URI, values, null, null);

 

构建uri

 

Uri contactUri = ContentUris.withAppendedId(ContactsContract.Data.CONTENT_URI,
Long.parseLong(contactId));
//带路径
Uri photoUri = Uri.withAppendedPath(contactUri, ContactsContract.Contacts.Photo.DATA15);

 此uri为content://com.android.contacts/contactId/data15

 

获得联系人图片

 

// 获得联系人图片,		读取的是Data类中的data15字段
Uri uri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI,
 Long.parseLong(contactId)); InputStream input =
 ContactsContract.Contacts.openContactPhotoInputStream(cr, uri);
 Bitmap contactPhoto = BitmapFactory.decodeStream(input);
 ImageView img=(ImageView)findViewById(R.id.img);
 img.setImageBitmap(contactPhoto);

 

 查询电话记录

 

// 查询所有
// Cursor cursor=resolver.query(CallLog.Calls.CONTENT_URI, null,
		// null,null, null);
// 查询指定号码
Cursor cursor=resolver.query(CallLog.Calls.CONTENT_URI, null,
		  "number=? and type=?", new String[]{"15555215556","2"}, null);
 while(cursor.moveToNext()){ //取得联系人名字 PhoneLookup.DISPLAY_NAME int nameFieldColumnIndex
		 =cursor.getColumnIndex(CallLog.Calls.CACHED_NAME); String
	contact=cursor.getString(nameFieldColumnIndex); 
//取得电话号码
 int numberFieldColumnIndex=cursor.getColumnIndex(PhoneLookup.NUMBER);
 String number=cursor.getString(numberFieldColumnIndex);

 

取得浏览器所有“书签”信息:content://browser/bookmarks

数据都存储在contract的data表中,每一个联系人项存储一行

另有两篇:一篇讲解contentProvider

http://www.cnblogs.com/not-code/archive/2011/06/24.html

另一篇讲解联系人

http://blog.csdn.net/wenlin56/article/details/6074437

你可能感兴趣的:(android)