有效的方式来获得图片的联系人信息 [英] Effective way to get contacts info with photo

查看:159
本文介绍了有效的方式来获得图片的联系人信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获得的所有联系人(我用的API拉特8)的一些基本信息。目前我使用这个code段

I want to get some basic info of all contacts(I use api lvl 8). Currently i use this code snippet

private List<ContactInfo> readContacts() {

    ContentResolver cr = getContentResolver();
    Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
            null, null, Phone.DISPLAY_NAME + " ASC");

    for (int i = 0; i < cur.getColumnCount(); i++) {

        Log.v("COlum", cur.getColumnName(i));

    }
    List<ContactInfo> temp = new ArrayList<ContactInfo>();
    if (cur.getCount() > 0) {

        while (cur.moveToNext()) {
            ContactInfo mContactInfo = new ContactInfo();
            String id = cur.getString(cur
                    .getColumnIndex(ContactsContract.Contacts._ID));
            mContactInfo.setId(Long.parseLong(id));
            String name = cur
                    .getString(cur
                            .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
            if (Integer
                    .parseInt(cur.getString(cur
                            .getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
                mContactInfo.setmDisplayName(name);

                // get the <span class="IL_AD" id="IL_AD7">phone
                // number</span>
                Cursor pCur = cr.query(
                        ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                        null,
                        ContactsContract.CommonDataKinds.Phone.CONTACT_ID
                                + " = ?", new String[] { id }, null);
                while (pCur.moveToNext()) {
                    String phone = pCur
                            .getString(pCur
                                    .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

                    mContactInfo.setmPhoneNumber(phone);
                }
                pCur.close();

                // get email and type

                Cursor emailCur = cr.query(
                        ContactsContract.CommonDataKinds.Email.CONTENT_URI,
                        null,
                        ContactsContract.CommonDataKinds.Email.CONTACT_ID
                                + " = ?", new String[] { id }, null);
                while (emailCur.moveToNext()) {
                    // This would allow you get several email <span
                    // class="IL_AD" id="IL_AD9">addresses</span>
                    // if the email addresses were stored in an array
                    String email = emailCur
                            .getString(emailCur
                                    .getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));

                    mContactInfo.setmEmail(email);
                }
                emailCur.close();

                temp.add(mContactInfo);
            }
        }
    }
    return temp;

}

和传递给自定义适配器(扩展baseadapter)。我用得到联系人的照片:

and pass to custom adapter (extended baseadapter). I get contact's photo using:

public static Bitmap loadContactPhoto(ContentResolver cr, long id) {
        Uri uri = ContentUris.withAppendedId(
                ContactsContract.Contacts.CONTENT_URI, id);
        InputStream input = openContactPhotoInputStream1(cr, uri);
        if (input == null) {
            return null;
        }
        return BitmapFactory.decodeStream(input);
}

我的手机有2个触点(有照片)上测试。我花了10秒〜获取在第一次运行时的所有联系。我试图迫使应用程序设置关闭并重新运行。这一次,它花了约2秒获得data.So我想知道获得接触信息的最有效方式。

I tested on my phone with 2x contacts (had photo). I took ~ 10s to fetch all contact at 1st runtime. I try force close in application settings and run again. This time it took ~2s to get data.So i want to know the most effective way to get contacts info.

我发现了一些类似SO的问题,但他们不需要照片。在android系统 接触
我试着用光标和光标适配器,但我不知道是什么查询在同一时间获得photo_uri +联系人姓名。

I found some similar SO questions but they dont need photo. contacts in android I tried use cursor and cursor adapter but i dont know what query to get photo_uri + contact name at the same time.

修改:我删除了所有getColumnIndex我可以出循环和项目只有我想列。性能较好(10秒=> 5秒)。

Edit: i removed all getColumnIndex i can out of loop and project only column i want. The performance is better(10s => 5s).

我想知道些什么:
更好地查询信息,并设置为我的ContactInfo模型或获得名称查询,电话号码,电子邮件,照片在同一时间的方式传递给光标适配器。

What i want to know : Better way to query info and set to my ContactInfo model or the query which get name, phone number, email, photo at the same time to pass to cursor adapter.

在此先感谢

推荐答案

我改成 CusorAdapter 和使用<一个href=\"http://grep$c$c.com/file/repository.grep$c$c.com/java/ext/com.google.android/android-apps/2.2_r1.1/com/android/contacts/ContactPhotoLoader.java\"相对=nofollow> ContactsPhotoLoader 从通讯录应用,提高性能。

I changed to CusorAdapter and use ContactsPhotoLoader from Contacts app and performance is improved.

这篇关于有效的方式来获得图片的联系人信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆