2014-01-28 64 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它所屬的接觸..

回來,如果您有更多問題。

相關問題