2012-07-02 44 views
0

我有問題,我的設置我的ListViewImageView接觸式圖像。我已經嘗試過所有從開發者網站到所有「所謂的」解決方案「,但它們都沒有效果。負荷聯繫人照片到列表視圖的Android

這是我CustomAdapter類填充我的ListViewhttp://pastebin.com/ABeK9nmT

這是我的活動持listviewhttp://pastebin.com/FcBDprfG

它的重要,要知道,該活動保持與周圍不可移動的對象listview。 我運行代碼時的問題是隻顯示默認的照片寄存器並且沒有顯示聯繫人。

當活動跑,logcat的顯示此爲每個6個聯繫人:

W/Resources(5997): Converting to string: TypedValue{t=0x12/d=0x0 a=3 r=0x7f080015}

我得不到任何錯誤。有沒有人明白我可能做錯了什麼?

請,沒有複製和粘貼從其他SO編碼「的解決方案。」我已經嘗試過所有這些,他們要麼是API較低,要麼從另一個答案中複製以獲得積分,要麼丟失太多數據。我正在使用2.3.3其中使用Contacts.Contract

+0

您參考鏈接斷開。 –

回答

1

我想推薦一些東西,也該,你可以嘗試,讓我知道,如果它仍然失敗的解決方案。

1)您使用的短信定製商爲您的應用程序,如果用戶更新自己的OS 4.0或以上,當我試着他們不力在他們的工作可能會失敗。

2)這將是更好,如果你嘗試查詢所有定製的提供者不getView(),因爲它會加快這一進程和ListView。

3)我覺得你的代碼的問題是,當你試圖取回接觸式圖像,正如我前面實施和犯規原來在博客中提到,不知道爲什麼,這是有點典型。

4)現在清水流量:

  • 您有您要查詢的電話號碼。
  • 現在我們將採用該號碼並查詢電話簿內容提供商的名稱。
  • 如果名不爲空我們可能會在獲得一定數量的特定ID進行(檢查是否可以直接讀取ID和做沒有取名字)
  • 如果的ContactID不爲空獲取圖片,並把它在你的ListView上。

不要忘記放置空檢查,並按照提到的代碼,如果失敗讓我知道。

5)代碼:

獲取聯繫人姓名:

/** 
* Method used to fetch CONTACT NAME FOR CONTACT NUMBER 
* 
* @param number :: Contact Number 
* @param context :: Activity context 
* @return   :: returns CONTACT-NAME for CONTACT NUMBER 
*/ 
private String getContactNameFromNumber(String number, Context context) 
{ 
    String contactName = null; 
    Uri contactUri = null; 
    String[] projection = null; 
    String selection = null; 
    Cursor cursorContactId = null; 

    /* 
    * Defining URI, Projection and Selection elements 
    */ 
    contactUri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number)); 
    projection = new String[]{Contacts._ID,Contacts.DISPLAY_NAME}; 
    selection = ContactsContract.CommonDataKinds.Phone.NUMBER + " = " + number; 

    /* 
    * Cursor iterator to fetch 
    * particular ID 
    */ 
    try 
    { 
     cursorContactId = context.getContentResolver().query(contactUri, projection, selection, null, null); 
     while (cursorContactId.moveToNext()) 
     { 
      contactName = cursorContactId.getString(cursorContactId.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)); 
     } 
    } 
    catch(Exception e) 
    { 
     e.printStackTrace(); 
    } 
    finally 
    { 
     cursorContactId.close(); 
    } 

    if(contactName != null) 
    { 
     return contactName; 
    } 
    else 
    { 
     return null; 
    } 
} 

獲取聯繫ID(如果名稱不):

/** 
* Fetching Contact from a Number 
* @param number - For which specific Id is required 
* @param context - current context 
* @return   - ID as String 
*/ 
private String getContactIdFromNumber(String number, Context context) 
{ 
    String contactId = null; 
    Uri contactUri = null; 
    String[] projection = null; 
    String selection = null; 
    Cursor cursorContactId = null; 

    /* 
    * Defining URI, Projection and Selection elements 
    */ 
    contactUri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number)); 
    projection = new String[]{Contacts._ID}; 
    selection = ContactsContract.CommonDataKinds.Phone.NUMBER + " = " + number; 

    /* 
    * Cursor iterator to fetch 
    * particular ID 
    */ 
    try 
    { 
     cursorContactId = context.getContentResolver().query(contactUri, projection, selection, null, null); 
     while (cursorContactId.moveToNext()) 
     { 
      contactId = cursorContactId.getString(cursorContactId.getColumnIndex(ContactsContract.Contacts._ID)); 
     } 
    } 
    catch(Exception e) 
    { 
     e.printStackTrace(); 
    } 
    finally 
    { 
     cursorContactId.close(); 
    } 

    if(contactId != null) 
    { 
     return contactId; 
    } 
    else 
    { 
     return null; 
    } 
} 

最後加載聯繫人圖片(如果ID不爲空ss ID & photo_id相同的ID值。 採取此代碼是從SO搜索各個環節)後:

/** 
* Fetching contact picture from PhoneBook if available 
* @param contentResolver - Current Content Resolver context 
* @param id    - Specific Contact Id as retrieved 
* @param photo_id   - Same as Contact Id 
* @return     - Contact Byte Array if present or null 
*/ 
private byte[] loadContactPhoto(ContentResolver contentResolver, String id, String photo_id) 
{ 
    byte[] result = null; 
    byte[] photoBytes = null; 

    /* 
    * Step 1 : Directly fetching with contactId or execute Step 2 
    */ 
    Uri uri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, Long.parseLong(id)); 
    InputStream input = ContactsContract.Contacts.openContactPhotoInputStream(contentResolver, uri); 
    /* 
    * Execute only if stream is not null 
    */ 
    if (input != null) 
    { 
     ByteArrayOutputStream bos = new ByteArrayOutputStream(); 
     try 
     { 
      int next = input.read(); 

      while (next > -1) 
      { 
       bos.write(next); 
       next = input.read(); 
      } 

      bos.flush(); 
     } 
     catch(Exception e) 
     { 
      e.printStackTrace(); 
     } 

     result = bos.toByteArray(); 

     return result; 
     // return BitmapFactory.decodeStream(input); // Open if want to return as Bitmap 
    } 
    else 
    { 
     Log.d("PHOTO","first try failed to load photo"); 
    } 

    /* 
    * Step 2: Image not fetched directly, fetching through traversing 
    */ 
    Uri photoUri = ContentUris.withAppendedId(ContactsContract.Data.CONTENT_URI, Long.parseLong(photo_id)); 
    Cursor cursorImageFetch = contentResolver.query(photoUri, new String[] {ContactsContract.CommonDataKinds.Photo.PHOTO}, null, null, null); 

    try 
    { 
     if (cursorImageFetch.moveToFirst()) 
      photoBytes = cursorImageFetch.getBlob(0); 
    } 
    catch (Exception e) 
    { 
     e.printStackTrace(); 

    } finally { 

     cursorImageFetch.close(); 
    }   

    if (photoBytes != null) 
    { 
     return photoBytes; 
     //return BitmapFactory.decodeByteArray(photoBytes,0,photoBytes.length); // Open if want to return as Bitmap 
    } 
    else 
    { 
     Log.d("PHOTO","Picture of the contact not available"); 
     return getByteArray(); 
    } 


} 
+0

它有效嗎?嘗試更換方法中的loadContactPhoto()? – iabhi