Вопрос

I used this code to retrieve a photo from one contact:

private Uri getPhotoUri(String idContact) {
        try {
            Cursor cur = getApplicationContext().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(idContact));
        return Uri.withAppendedPath(person, ContactsContract.Contacts.Photo.CONTENT_DIRECTORY);
    }

So, the problem is that with this code I can retrieve a photo with low resolution. How can i do to get a better photo?

Maybe I am not supposed to use CONTENT_DIRECTORY.

Thank you.

Это было полезно?

Решение

This code works pretty well to get a high-res photo:

 Uri my_contact_Uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_URI, String.valueOf(id));
 InputStream photo_stream = ContactsContract.Contacts.openContactPhotoInputStream(cr, my_contact_Uri, true);
 BufferedInputStream buf = new BufferedInputStream(photo_stream);
 Bitmap my_btmp = BitmapFactory.decodeStream(buf);
 buf.close();
 return my_btmp;

Also, just fyi, high-res contact photos didn't exist before API 14, so if you're running it on a AVD/Device with API 13 or less, you can't get a high-res photo.

Look at this question: Get high-res contact photo as bitmap below API level 14 Android for more info.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top