android获取手机联系人信息

/*
* 手机联系人定义常量
*/
public static final String[] PHONES_PROJECTION = new String[]
{
Phone.DISPLAY_NAME, //联系人姓名
Phone.NUMBER, //电话号码
Photo.PHOTO_ID, //联系人头像
Phone.CONTACT_ID //ID
};

public static final int PHONES_DISPLAY_NAME_INDEX = 0;

public static final int PHONES_NUMBER_INDEX = 1;

public static final int PHONES_PHOTO_ID_INDEX = 2;

public static final int PHONES_CONTACT_ID_INDEX = 3;


/**
* 获取联系人信息
*
* @param
* @return void
* @throws
* @since TianTian
*/
private void getContactInformation()
{
Uri contact_uri = Phone.CONTENT_URI; //获得联系人默认uri
ContentResolver resolver = this.getContentResolver(); //获得ContentResolver对象
Cursor cursor = resolver.query(contact_uri,Constant.PHONES_PROJECTION, null, null, null); //获取电话本中开始一项光标
contacts = new ArrayList<ContactPeson>();
if (null != cursor)
{
while (cursor.moveToNext())
{
ContactPeson peson = new ContactPeson();

/*
* 获取电话号码
*/
String number = cursor.getString(Constant.PHONES_NUMBER_INDEX);
/*
* 当手机号码为空的或者为空字段 跳过当前循环
*/
if (TextUtils.isEmpty(number)) continue;

/*
* 得到联系人名称
*/
String name = cursor.getString(Constant.PHONES_DISPLAY_NAME_INDEX);
/*
* 得到联系人ID
*/
Long id = cursor.getLong(Constant.PHONES_CONTACT_ID_INDEX);
/*
* 得到联系人头像ID
*/
Long photo = cursor.getLong(Constant.PHONES_PHOTO_ID_INDEX);
Bitmap contactPhoto = null;
if( photo > 0 )
{
Uri uri =ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, id);
InputStream input = ContactsContract.Contacts.openContactPhotoInputStream(resolver, uri);
contactPhoto = BitmapFactory.decodeStream(input);
}
else
{
contactPhoto = BitmapFactory.decodeResource(getResources(), R.drawable.jt3);
}

peson.setContact_number(number);
peson.setContact_name(name);
peson.setContact_photo(contactPhoto);

contacts.add(peson);

}

cursor.close();
}

}


model:

private String contact_id;

private Bitmap contact_photo; //联系人照片

private String contact_name; //联系人名字

private String contact_number; //联系人电话号码
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值