如何将所有联系人与给定联系人合并

时间:2014-01-28 21:45:48

标签: android android-sqlite contacts photo

我正在尝试显示联系人列表以及与他们相关联的照片,但是当我转到手机(SG2)默认联系人列表时,我看到大多数没有照片的联系人,尽管他们有照片。我认为这是因为其中一些联系人实际上是合并的,图片来自列表中未显示的(原始?)联系人,所以问题是 - 如何让所有联系人与给定的联系人合并?我尝试使用SQLite,但我找不到任何唯一标识合并联系人的列。

从代码的角度来看,我开始使用这个方法: protected Bitmap getImageByContactId(final Context appContext,final String strContactId)     {         位图bmContactImage = null;

    Uri contactUri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, Long.parseLong(strContactId));
    Uri photoUri = Uri.withAppendedPath(contactUri, ContactsContract.Contacts.Photo.CONTENT_DIRECTORY);

    InputStream image = null;
    try {
        image = appContext.getContentResolver().openInputStream(photoUri);
        bmContactImage = BitmapFactory.decodeStream(image);

        try {
            image.close();
        } catch (IOException e ) {
            e.printStackTrace();
        }

    } catch (FileNotFoundException e1) {
        // This can be ignored because the Uri is generic and photo may not really exist
    }

    return bmContactImage;
}

但这只会返回与原始搜索条件匹配的“原始”联系人相关的照片(在我的情况下 - 联系人ID) - 如果图片与链接到原始联系人的联系人相关联,则不会。

如果我只能获得与原始联系人链接/关联/合并的联系人的(原始?)ID,我希望能够完成以下工作:但我似乎无法找到找到的方法那些“其他”接触者......

public static Bitmap loadContactPhoto(ContentResolver cr, long id) {
    Uri uri = Uri.parse(String.format("content://com.android.contacts/contacts/%s", id));
    InputStream input = ContactsContract.Contacts.openContactPhotoInputStream(cr, uri);
    if (input == null) {
        return null;
    }
    return BitmapFactory.decodeStream(input);
}

底线 - 我要做的是显示联系人的照片,无论是来自符合搜索条件的联系人 - 还是与该联系人合并的任何联系人 - 并且任何解决方案都将受到赞赏!

谢谢!

1 个答案:

答案 0 :(得分:0)

(我从我们的facebook页面来到这里..)

以下是我从内容解析器获取所有联系人的方式:

public void addContactsMultiple()
{
    Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
            null, null, null, null);
     if (cur.getCount() > 0) {          
           String id="";//Contact ID
           String name="";//Contact Name
           id = "";
           id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
           name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
           if(Integer.parseInt
           (cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
               //Getting the Contact Image
               if(cur.getString(cur
                        .getColumnIndex(ContactsContract.CommonDataKinds.Phone.PHOTO_URI)) !=  null)
               {
                   imagePath = cur
                      .getString(cur
                        .getColumnIndex(ContactsContract.CommonDataKinds.Phone.PHOTO_URI));
               }
               else {imagePath="none";}//If there is no contact image
          }

       }
       cur.close();
       }

在我的代码中,您获取图像URI,而不是将其放在Contact对象中,而不是使用

      ImageView image = (ImageView)findViewById(R.id.image)
      image.setImageURI(Uri.parse(imagePath));

或者您可以使用Picasso Image Loader: Picasso

而不是使用此代码:

           Picasso.with(context)
           .load(imagePath)
           .skipMemoryCache()
           .into(image );

如您所见,我不使用BitMaps或decodeStream。 您只需要照片的uri(将图片定向到Android图库)

如果您从同一个名称获得多个联系人,则可以使用此代码来激发他们:

//(mContactList) is the ArrayList of contacts object I have created)
 HashSet<Contact> hs = new HashSet<Contact>();
        hs.addAll(mContactList);
        mContactList.clear();
        mContactList.addAll(hs);

此代码将删除重复

通过一些调整,您可以将图像uri发送给它所属的联系人。

如果你有更多问题,请回来。