像whatsapp一样加载联系人

时间:2013-12-30 05:55:22

标签: android android-contentprovider android-contacts android-cursoradapter

我正在努力获取活动中的联系人列表。我想为联系人检索联系人照片,如果他们有个人资料选择,就像在whatsapp中一样。在whatsapp中,即使我的移动设备中没有它们的照片,也会加载联系人图像。任何人都可以指导这部分。我真的需要这个。我的编码部分如下。我尝试了一切可能的方式来获取联系人照片。我正在使用自定义光标适配器来加载联系人。

class contactAdapter extends CursorAdapter{

        String Name, phoneNumber;
        private Cursor cursor;
        private Context ccontext;
        private LayoutInflater inflater;

        public contactAdapter(Context context, Cursor c) {
            super(context, c);
            // TODO Auto-generated constructor stub
            cursor = c;
            ccontext = context;
            inflater = LayoutInflater.from(context);
            inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }

        @SuppressLint("InlinedApi")
        @SuppressWarnings("deprecation")
        @Override
        public void bindView(View view, Context arg1, Cursor arg2) {
            // TODO Auto-generated method stub

            ViewHolder holder = (ViewHolder) view.getTag();
            if (holder == null) {
                holder = new ViewHolder();
                holder.contactsImage = (ImageView) view.findViewById(R.id.contact_image);
                holder.ContactName = (TextView) view.findViewById(R.id.contact_name);
                holder.contactCheck = (CheckBox) view.findViewById(R.id.contact_check);
                view.setTag(holder);
                holder.contactCheck.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        cb = (CheckBox) v;
                        cb.setChecked(cb.isChecked());
//                      ContactPerson selected = (ContactPerson)cb.getTag();
                        Log.d("selcted", cb.getTag().toString());
                        if(cb.isChecked()){

                            phoneID.add(cb.getTag().toString());


                        }
                    }

                }); 
            }else{
                holder = (ViewHolder) view.getTag();
            }
            Uri uri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI,Long.parseLong(String.valueOf((cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.PHOTO_ID)))));
            Log.d("ImagePath", uri.toString());
            InputStream input = ContactsContract.Contacts.openContactPhotoInputStream(ccontext.getContentResolver(), uri);
            Bitmap photo =  BitmapFactory.decodeStream(input);
            holder.contactsImage.setImageBitmap(photo);
            holder.ContactName.setText(cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)));
            holder.contactCheck.setTag(cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID)));
            holder.contactCheck.setChecked(false);


        }

        @Override
        public View newView(Context arg0, Cursor arg1, ViewGroup arg2) {
            // TODO Auto-generated method stub
            return inflater.inflate(R.layout.checkbox_item, arg2, false);
        }

        @Override
        public View getView(int arg0, View arg1, ViewGroup arg2) {
            // TODO Auto-generated method stub
//          Log.d("don't know", cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)));
            return super.getView(arg0, arg1, arg2);
        }

    }

1 个答案:

答案 0 :(得分:1)

之前已经有人问过了。如果要加载联系人图像,其代码如下

public Uri getPhotoUri(String contactId) {
            try {
                Cursor cur = ccontext.getContentResolver().query(
                        ContactsContract.Data.CONTENT_URI,
                        null,
                        ContactsContract.Data.CONTACT_ID + "=" + contactId + " AND "
                                + ContactsContract.Data.MIMETYPE + "='"
                                + ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE + "'", null,
                        null);
                if (cur != null) {
                    if (!cur.moveToFirst()) {
                        return null; // no photo
                    }
                } else {
                    return null; // error in cursor process
                }
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
            Uri person = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, Long
                    .parseLong(contactId));
            return Uri.withAppendedPath(person, ContactsContract.Contacts.Photo.CONTENT_DIRECTORY);
        }

并检查返回的结果是否包含像这样的联系人图像

 Uri u = getPhotoUri(cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID)));
            if (u != null) {
                    holder.contactsImage.setImageURI(u);
            } else {
                    holder.contactsImage.setImageResource(R.drawable.ic_launcher);
            }