android4.0获取联系人个人档案库的邮件地址和头像

public static Bitmap getPhoto(Context context, String mailAddress) {
        Bitmap selfContactPhoto = null;
        ContentResolver resolver = context.getContentResolver();

        String[] CONTACTS_COLUMNS = new String[] {
                ContactsContract.Contacts._ID,//查询的ID,主键唯一
                ContactsContract.Contacts.PHOTO_ID,//查询图片的ID
                ContactsContract.Contacts.PHOTO_FILE_ID,
                ContactsContract.Contacts.LOOKUP_KEY//用来转换地址获取邮件地址
            };

           //查询游标
        Cursor cur = resolver.query(Profile.CONTENT_URI, CONTACTS_COLUMNS,
                null, null, null);
        Uri lookupUri = null;
        try {
            if (cur.moveToFirst()) {
                final long contactId = cur.getLong(ContactQuery.Contacts._ID);
                final long photoId = cur.getLong(ContactQuery.Contacts.PHOTO_ID);
                final long photoFileId = cur.getLong(ContactQuery.Contacts.PHOTO_FILE_ID);
                final String lookupKey = cur.getString(ContactQuery.Contacts.LOOKUP_KEY);

                //得到uri地址
                lookupUri = Contacts.getLookupUri(contactId, lookupKey);

                //得到邮件地址的列表
                List<String> mailList = loadProfileMailAdress(resolver, lookupUri);
                if (mailList.contains(mailAddress)) {
                    if (photoId > 0) {//得到头像
                        Uri uri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, contactId);
                        InputStream input = ContactsContract.Contacts.openContactPhotoInputStream(resolver, uri);
                        selfContactPhoto = BitmapFactory.decodeStream(input);
                    }
                    return selfContactPhoto;
                }
            }
        } finally {
            cur.close();
        }
        return selfContactPhoto;
    }

 

private static List<String> loadProfileMailAdress(ContentResolver resolver, Uri contactUri) {
        Uri entityUri = Uri.withAppendedPath(contactUri, Contacts.Entity.CONTENT_DIRECTORY);
        Cursor cursor = resolver.query(entityUri,
                ContactQuery.Data.DATA_COLUMNS,
                ContactsContract.Data.MIMETYPE + "=?",
                new String[] {Email.CONTENT_ITEM_TYPE},
                Contacts.Entity.RAW_CONTACT_ID);
        List<String> addressList = new ArrayList<String>();
        try {
            while (cursor.moveToNext()) {
                String address = cursor.getString(ContactQuery.Data.DATA1);
                addressList.add(address);
            }
        } finally {
            cursor.close();
        }
        return addressList;
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值