【问题标题】:loading contacts just like whatsapp像whatsapp一样加载联系人
【发布时间】:2014-01-17 00:28:32
【问题描述】:

我正在努力获取活动中的联系人列表。我想检索联系人的联系人照片,如果他们有个人资料选择,就像在 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);
        }

    }

【问题讨论】:

  • Whatsapp 显示的不是您手机上的联系人的个人资料图片,而是用户设置为他们自己的 whatsapp 个人资料的个人资料。
  • @nikvs:谢谢。现在我明白了。您能否告诉我联系人图像在手机中的确切存储位置。因为在我的三星手机中,有图片的联系人会显示在联系人列表中。但我也无法检索这些图像。
  • 有一个 Uri,请查看上面的链接,我发布了它的综合指南。
  • @user2955143 你为什么要自我破坏你的问题?

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


【解决方案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

 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);
            }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-21
    • 2017-12-12
    • 2015-08-04
    • 2011-03-04
    • 2017-09-07
    相关资源
    最近更新 更多