获取联系人信息方法

来源:互联网 发布:东方不败 知乎 编辑:程序博客网 时间:2024/05/14 10:05
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;    }}
1 0
原创粉丝点击