当前位置:操作系统 > 安卓/Android >>

联系人头像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
补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,