2016-08-27 75 views
0

所以我一直在搜索整個Stackoverflow和Android官方文檔關於檢索聯繫人的照片,我根本不明白它。我對Java很新,所以除了「統一資源標識符」之外,我甚至都不明白InputStream是什麼或者是什麼URI。因此,我只是複製並粘貼Android文檔中的代碼,因爲我認爲它沒有辦法出錯。事實證明,每次我嘗試打開一張照片時,它都會返回null。谷歌肯定確實讓聯繫人很難。就像有很多簡單的刪除聯繫人的名字一樣,更不用說照片了。聯繫人照片未找到?

下面是一些代碼:
openPhoto()函數

private InputStream openPhoto(long contactId) { 
    Uri contactUri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, contactId); 
    Uri photoUri = Uri.withAppendedPath(contactUri, ContactsContract.Contacts.Photo.CONTENT_DIRECTORY); 

    Cursor cursor = getContentResolver().query(photoUri, 
      new String[] {ContactsContract.Contacts.Photo.PHOTO}, null, null, null); 

    if(cursor == null) { 
     return null; 
    } 

    try { 
     if(cursor.moveToFirst()) { 
      byte[] data = cursor.getBlob(0); 

      if(data != null) 
       return new ByteArrayInputStream(data); 
     } 
    } finally { 
     cursor.close(); 
    } 

    return null; 
} 

區,其中照片被打開

... 
InputStream stream = openPhoto(c.getID()); 
if(stream != null) 
    Log.i("PHOTO", stream.toString()); 
else 
    Log.i("NULL", "PHOTO IS NULL"); 
... 

在上面的代碼中,記錄器總是保持登錄「NULL 「:」PHOTO IS NULL「。所以,爲什麼在這裏找不到聯繫人的照片?

編輯: 如果您有答案,請解釋發生了什麼。我很欣賞任何答案,但我想知道發生了什麼。到目前爲止,這仍然沒有解決。所以,如果你有答案,只要解釋原因。謝謝。

回答

0

//返回照片URI

public Uri getPhotoUri(String idContact) { 
    try { 
     Cursor cur = this.ctx.getContentResolver().query(
       ContactsContract.Data.CONTENT_URI, 
       null, 
       ContactsContract.Data.CONTACT_ID + "=" + idContact+ " 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(getId())); 
    return Uri.withAppendedPath(person, ContactsContract.Contacts.Photo.CONTENT_DIRECTORY); 
} 

//實現

Uri u = objItem.getPhotoUri(); 
if (u != null) { 
     mPhotoView.setImageURI(u); 
} else { 
     mPhotoView.setImageResource(R.drawable.ic_contact_picture_2); 
} 
0

使用此功能來獲取選定聯繫人的位圖圖像。

private void retrieveContactPhoto() { 
    Bitmap photo = null; 
    InputStream inputStream = null; 
    try { 
     inputStream = ContactsContract.Contacts.openContactPhotoInputStream(getContentResolver(), 
       ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, new Long(contactID))); 
     if (inputStream != null) { 
      photo = BitmapFactory.decodeStream(inputStream); 
      ImageView imageView = (ImageView) findViewById(R.id.img_contact); 
      imageView.setImageBitmap(photo); 
     } 
    } finally { 
     if (inputStream != null) 
      try { 
       inputStream.close(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
    } 
}