代码改变世界

联系人头像 android

2012-06-25 16:21  htc开发  阅读(480)  评论(0编辑  收藏  举报

  1. <pre class="java" name="code">ContentResolver cr = getContentResolver();  
  2. Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null"DISPLAY_NAME = '" + "gm" + "'"null,  
  3.                 null);  
  4. if (cursor.moveToFirst()) {  
  5. //获得联系人的id            
  6. String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));  
  7.   
  8. //修改联系人的头像  
  9. Bitmap sourceBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.img2);  
  10. final ByteArrayOutputStream os = new ByteArrayOutputStream();  
  11. // 将Bitmap压缩成PNG编码,质量为100%存储  
  12. sourceBitmap.compress(Bitmap.CompressFormat.PNG, 100, os);  
  13. byte[] avatar =os.toByteArray();  
  14. ContentValues values = new ContentValues();  
  15. values.put(Data.RAW_CONTACT_ID, contactId);  
  16. values.put(Data.MIMETYPE, Photo.CONTENT_ITEM_TYPE);  
  17. values.put(Photo.PHOTO, avatar);  
  18. getContentResolver().update(Data.CONTENT_URI, values, nullnull);</pre>  

 

构建uri

 


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

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

 

获得联系人图片

 

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

 

 查询电话记录

 

  1. // 查询所有  
  2. // Cursor cursor=resolver.query(CallLog.Calls.CONTENT_URI, null,  
  3.         // null,null, null);  
  4. // 查询指定号码  
  5. Cursor cursor=resolver.query(CallLog.Calls.CONTENT_URI, null,  
  6.           "number=? and type=?"new String[]{"15555215556","2"}, null);  
  7.  while(cursor.moveToNext()){ //取得联系人名字 PhoneLookup.DISPLAY_NAME int nameFieldColumnIndex  
  8.          =cursor.getColumnIndex(CallLog.Calls.CACHED_NAME); String  
  9.     contact=cursor.getString(nameFieldColumnIndex);   
  10. //取得电话号码  
  11.  int numberFieldColumnIndex=cursor.getColumnIndex(PhoneLookup.NUMBER);  
  12.  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