简体   繁体   中英

Get high resolution contact photo

Does anyone know how to retrieve the high quality photo of a telephonebook contact (user)? All of my tries resulted in low quality images...

It doesn't matter If I retrieve it via "openContactPhotoInputStream" or via an "Uri", both are in low quality. It looks like Thumbnails.

My approaches:

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 is set to android version 4.3. Thank you.

Use the other overloaded method of openContactPhotoInputStream (added in API level 14), the one with the boolean parameter indicating that you prefer high res photos.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM