灯火互联
管理员
管理员
  • 注册日期2011-07-27
  • 发帖数41778
  • QQ
  • 火币41290枚
  • 粉丝1086
  • 关注100
  • 终身成就奖
  • 最爱沙发
  • 忠实会员
  • 灌水天才奖
  • 贴图大师奖
  • 原创先锋奖
  • 特殊贡献奖
  • 宣传大使奖
  • 优秀斑竹奖
  • 社区明星
阅读:3469回复:0

联系人头像android

楼主#
更多 发布于:2012-09-06 14:06




<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

喜欢0 评分0
游客

返回顶部