簡體   English   中英

未找到聯系人照片?

[英]Contact Photo not being found?

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

在上面的代碼中,Logger 總是記錄 "NULL" : "PHOTO IS NULL"。 那么,為什么這里找不到聯系人的照片?

編輯:如果你有答案,請解釋發生了什么。 我很感激任何答案,但我想了解發生了什么。 到目前為止,這仍然沒有解決。 因此,如果您有答案,請解釋原因。 謝謝。

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

使用此功能可檢索所選聯系人的位圖圖像。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM