1

I'm trying to get a contact's full-size image given a contact id, I came up with this :

private Bitmap getUserPhoto(long contactId) {
    Uri contactUri = ContentUris.withAppendedId(Contacts.CONTENT_URI,
        contactId);
    Uri displayPhotoUri = Uri.withAppendedPath(contactUri,
        Contacts.Photo.DISPLAY_PHOTO);
    try {
        AssetFileDescriptor fd = getContentResolver()
            .openAssetFileDescriptor(displayPhotoUri, "r");
        return BitmapFactory.decodeStream(fd.createInputStream());
    } catch (IOException e) {
        return null;
    }
}

but the image size is too small (Thumbnail)

how can I get the full-size photo of a contact?

Thanks

mmBs
  • 8,421
  • 6
  • 38
  • 46
John Smith
  • 11
  • 2

2 Answers2

2

You can use openContactPhotoInputStream(ContentResolver cr, Uri contactUri, boolean preferHighres), if you have original image in your phone. And set preferHighres TRUE.

feohoTl
  • 65
  • 7
0

You can resize Bitmap

private Bitmap getUserPhoto(long contactId) {
    Uri contactUri = ContentUris.withAppendedId(Contacts.CONTENT_URI,
        contactId);
    Uri displayPhotoUri = Uri.withAppendedPath(contactUri,
        Contacts.Photo.DISPLAY_PHOTO);
    try {
        AssetFileDescriptor fd = getContentResolver()
            .openAssetFileDescriptor(displayPhotoUri, "r");
        return Bitmap.createScaledBitmap(BitmapFactory.decodeStream(fd.createInputStream()), newWidth, newHeight, true);
    } catch (IOException e) {
        return null;
    }
}    

newWidth and newHeight are the Int values.

manivannan
  • 622
  • 1
  • 4
  • 17
  • otherwise u can use CONTENT_MAX_DIMENSIONS_URI instead of CONTENT_URI like this :'ContentUris.withAppendedId(Contacts.CONTENT_MAX_DIMENSIONS_URI, contactId);' – manivannan Aug 23 '13 at 13:20
  • no suck field CONTENT_MAX_DIMENSIONS_URI in Contacts, it does exist in DisplayPhoto, anyways this can't help me since it requires min api 14, thanks anyway – John Smith Aug 23 '13 at 13:40