package com.me.contacts.db;
import java.io.InputStream;
import java.util.ArrayList;
import com.me.contacts.bean.ContactsBean;
import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.provider.ContactsContract.Contacts;
/**
* ClassName:ContactsDAO <br/>
* Function: 获取联系人信息方法. <br/>
* Date: 2016年12月19日 下午7:50:05 <br/>
*
* @author duc
* @version
*/
public class ContactsDAO {
public static ArrayList<ContactsBean> getAllContacts(Context context) {
ArrayList<ContactsBean> datas = new ArrayList<ContactsBean>();
ContentResolver cr = context.getContentResolver();
// 联系人Uri
Uri uri = Phone.CONTENT_URI;
String[] projection = new String[] {Phone.DISPLAY_NAME, // 名字
Phone.NUMBER, // 号码
Phone.CONTACT_ID}; // 图片id
Cursor cursor = cr.query(uri, projection, null, null, null);
if (cursor != null) {
while (cursor.moveToNext()) {
//
String name = cursor.getString(0);
String num = cursor.getString(1);
// 联系人头像图片id
String id = cursor.getString(2);
ContactsBean bean = new ContactsBean();
bean.name = name;
bean.num = num;
bean.id = id;
datas.add(bean);
}
cursor.close();
}
return datas;
}
public static Bitmap getContactsPhoto(Context context, String id) {
ContentResolver cr = context.getContentResolver();
Uri contactUri = Uri.withAppendedPath(Contacts.CONTENT_URI, id);
InputStream is = Contacts.openContactPhotoInputStream(cr, contactUri);
Bitmap bitmap = BitmapFactory.decodeStream(is);
return bitmap;
}
}
获取联系人信息方法
最新推荐文章于 2023-03-08 20:12:56 发布