簡體   English   中英

獲取高分辨率聯系人照片

[英]Get high resolution contact photo

有誰知道如何檢索電話簿聯系人(用戶)的高質量照片? 我所有的嘗試都導致圖像質量低下...

沒關系,如果我是通過“ openContactPhotoInputStream”或“ Uri”檢索到的,則兩者的質量都不高。 它看起來像縮略圖。

我的方法:

1

private void retrieveContactPhoto(ImageView imgView, Long contactId) {

    Bitmap photo = null;

    try {
        InputStream inputStream = ContactsContract.Contacts
                .openContactPhotoInputStream(getContentResolver(),
                        ContentUris.withAppendedId(
                                ContactsContract.Contacts.CONTENT_URI,
                                contactId));

        if (inputStream != null) {
            photo = BitmapFactory.decodeStream(inputStream);
            imgView.setImageBitmap(photo);
        }

        assert inputStream != null;
        inputStream.close();

    } catch (IOException e) {
        e.printStackTrace();
    }

}

2

public static Uri getPhotoUri(long contactId) {
    ContentResolver contentResolver = MainActivity.instance
            .getContentResolver();

    try {
        Cursor cursor = contentResolver
                .query(ContactsContract.Data.CONTENT_URI,
                        null,
                        ContactsContract.Data.CONTACT_ID
                                + "="
                                + contactId
                                + " AND "

                                + ContactsContract.Data.MIMETYPE
                                + "='"
                                + ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE
                                + "'", null, null);

        if (cursor != null) {
            if (!cursor.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, contactId);
    return Uri.withAppendedPath(person,
            ContactsContract.Contacts.Photo.CONTENT_DIRECTORY);
}

Min SDK設置為android 4.3版。 謝謝。

使用openContactPhotoInputStream的另一種重載方法(在API級別14中添加),該方法的布爾值指示您更喜歡高分辨率照片。

http://developer.android.com/reference/android/provider/ContactsContract.Contacts.html#openContactPhotoInputStream(android.content.ContentResolver、android.net.Uri ,布爾值)

public static InputStream openContactPhotoInputStream
        (ContentResolver cr, Uri contactUri, boolean preferHighres)

暫無
暫無

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

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