2014-01-28 65 views
0

我试图显示联系人列表以及与其关联的照片,但是我看到大多数没有照片的联系人,虽然他们在我去手机的(SG2)默认联系人列表。我想这是因为其中一些联系人实际上是合并的,并且图片来自未在列表中显示的(原始?)联系人,所以问题是 - 我如何获得与给定联系人合并的所有联系人?我试着玩SQLite,但是我找不到任何能够唯一标识合并联系人的列。如何获取与给定联系人合并的所有联系人

从代码的角度来看,我开始与这个方法: 保护位图getImageByContactId(最终上下文appContext,最终字符串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); 
} 

底线 - 我想要做的是显示联系人的照片,无论是从搜索条件匹配的接触 - 或者任何的联系人与该联系人合并 - 任何解决方案将不胜感激!

谢谢!

+0

如果你发布你的代码,它会更容易尝试和协助你... – Muzikant

+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,比把它在您的联系对象,比你可以使用

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

,或者您可以使用毕加索图像装载机: Picasso

,比使用此代码:

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

,你可以看到,我不使用位图或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它所属的接触..

回来,如果您有更多问题。

相关问题