【问题标题】:Contact Photo not being found?联系人照片未找到?
【发布时间】:2026-01-25 09:10:01
【问题描述】:

我一直在 * 和 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");
...

在上面的代码中,Logger 始终保持记录“NULL”:“PHOTO IS NULL”。那么,为什么这里找不到联系人的照片?

编辑:如果您有答案,请解释发生了什么。我很感激任何答案,但我想了解发生了什么。到目前为止,这仍然没有解决。因此,如果您有答案,请解释原因。谢谢。

【问题讨论】:

    标签: java android android-contacts


    【解决方案1】:

    //返回照片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);
    }
    

    【讨论】:

      【解决方案2】:

      使用此函数检索所选联系人的位图图像。

      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();
                  }
          }
      }
      

      【讨论】: