聯系人頭像android

<pre class="java" name="code">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);</pre> 
 
構建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); 
 

作者:hahashui123

發佈留言