android获取手机联系人信息(电话,邮箱,姓名,头像)

  在我们开发应用程序时,通常都会用到获取手机联系人信息这一十分常用的功能,最近项目里也要实现此功能,想到以后的APP还十分可能还有此功能,就干脆把这个小功能放到一个类中去,这样以后再遇到这个需求就不需要再去写代码了,直接把这个类拷过来就可以用了.

  以下是获取联系人demo的效果图,比较简陋,能说明问题就好.

 

以一个列表的形式显示所有联系,如果有头像则显示头像,没有头像则显示APP默认icon..

下面是封装好的类代码:

/**
 * @author renzhiqiang 获取手机联系人姓名,电话,邮箱,头像
 */
public class GetContacts {

    private static ArrayList<MyContacts> getAllContacts(Context context) {

        ArrayList<MyContacts> contacts = new ArrayList<MyContacts>();

        Cursor cursor = context.getContentResolver().query(
                ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
        while (cursor.moveToNext()) {

            //新建一个联系人实例
            MyContacts temp = new MyContacts();
            String contactId = cursor.getString(cursor
                    .getColumnIndex(ContactsContract.Contacts._ID));
            String name = cursor.getString(cursor
                    .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
            temp.name = name;
            
            //获取联系人所有电话号
            Cursor phones = context.getContentResolver().query(
                    ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                    null,
                    ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = "
                            + contactId, null, null);
            while (phones.moveToNext()) {

                String phoneNumber = phones
                        .getString(phones
                                .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                temp.phones.add(phoneNumber);
            }
            phones.close();
            //获取联系人所有邮箱.
            Cursor emails = context.getContentResolver().query(
                    ContactsContract.CommonDataKinds.Email.CONTENT_URI,
                    null,
                    ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = "
                            + contactId, null, null);

            while (emails.moveToNext()) {
                String email = emails
                        .getString(emails
                                .getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
                temp.emails.add(email);
            }
            emails.close();

            // 获取联系人头像
            temp.photo = getContactPhoto(context, contactId,
                    R.drawable.ic_launcher);
            contacts.add(temp);
        }
        return contacts;
    }

    /**
     * 获取手机联系人头像
     *
     * @param c
     * @param personId
     * @param defaultIco
     * @return
     */
    private static Bitmap getContactPhoto(Context c, String personId,
            int defaultIco) {
        byte[] data = new byte[0];
        Uri u = Uri.parse("content://com.android.contacts/data");
        String where = "raw_contact_id = " + personId
                + " AND mimetype ='vnd.android.cursor.item/photo'";
        Cursor cursor = c.getContentResolver()
                .query(u, null, where, null, null);
        if (cursor.moveToFirst()) {
            data = cursor.getBlob(cursor.getColumnIndex("data15"));
        }
        cursor.close();
        if (data == null || data.length == 0) {
            return BitmapFactory.decodeResource(c.getResources(), defaultIco);
        } else
            return BitmapFactory.decodeByteArray(data, 0, data.length);
    }
}
没有任何难点,只是做了一个小小的封装,使得以后不必再重复的去实现.


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值