Amira Elsayed Ismail
Amira Elsayed Ismail

Reputation: 9414

Effective way to load contacts with image in android

I have the following code to load all contacts with their pictures

public static void getAllContactWithNumberAndNameAndPhoto(Context context,
            ArrayList<ContactInfo> mContactList, boolean starred) {

        ContentResolver cr = context.getContentResolver();

        Cursor cur = null;
        if (starred == true) {
            cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
                    "starred=?", new String[] { "1" }, null);
        } else {

            cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null,
                    null, null);
        }
        if (cur.getCount() > 0) {
            while (cur.moveToNext()) {

                ContactInfo item = new ContactInfo();
                String id = cur.getString(cur
                        .getColumnIndex(ContactsContract.Contacts._ID));
                String name = cur
                        .getString(cur
                                .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                // Uri photo = PhoneUtils.getPhotoUriFromID(context, id);
                String starredValue = cur.getString(cur
                        .getColumnIndex(ContactsContract.Contacts.STARRED));
                boolean isFav = false;
                if (starredValue.equals("1"))
                    isFav = true;

                if (Integer
                        .parseInt(cur.getString(cur
                                .getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
                    Cursor pCur = cr.query(
                            ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                            null,
                            ContactsContract.CommonDataKinds.Phone.CONTACT_ID
                                    + " = ?", new String[] { id }, null);
                    while (pCur.moveToNext()) {

                        String phoneNo = pCur
                                .getString(pCur
                                        .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                        item.addPhone(removeCharactersFromPhoneNumber(phoneNo));
                    }
                    pCur.close();

                    // if (photo != null) {
                    //
                    // item.setPhoto(photo.toString());
                    // }

                    item.setName(name);
                    item.setFavorite(isFav);
                    item.setRecent(false);

                    mContactList.add(item);
                }
            }
            cur.close();
        }
    }

and the following code to load contact picture Uri

public static Uri getPhotoUriFromID(Context context, String id) {
        try {
            Cursor cur = context
                    .getContentResolver()
                    .query(ContactsContract.Data.CONTENT_URI,
                            null,
                            ContactsContract.Data.CONTACT_ID
                                    + "="
                                    + id
                                    + " AND "
                                    + ContactsContract.Data.MIMETYPE
                                    + "='"
                                    + ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE
                                    + "'", null, null);
            if (cur != null) {
                if (!cur.moveToFirst()) {
                    return null;
                }
            } else {
                return null;
            }
            cur.close();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        Uri person = ContentUris.withAppendedId(
                ContactsContract.Contacts.CONTENT_URI, Long.parseLong(id));
        return Uri.withAppendedPath(person,
                ContactsContract.Contacts.Photo.CONTENT_DIRECTORY);
    }

every thing is working fine but too slow about 40 second for 1000 contact, when I comment the part of loading image it takes 18 sec, so how can I load image uri in the same query of loading contact to minimize the waiting time of the user.

Upvotes: 0

Views: 523

Answers (1)

Nitesh Kumar
Nitesh Kumar

Reputation: 5440

For displaying the images of contacts you can use Universal Image Loader library. It's great for handling multiple images. You can use a default image to display initially and as soon as the contact's image is loaded the library will display that image.

This is how you display image in this library:

ImageLoader.getInstance().displayImage(img, imageView, options);

where img is the image, imageView is the ImageView where you want to display that image, options is the object of DisplayImageOptions of UniversalImageLoader library.

Upvotes: 2

Related Questions