Search code examples
androidphotosrawcontacts

Get a URI for a RawContact photo in Android


I know I can get the URI of a contact's photo using:

Uri person = ContentUris.withAppendedId(Contacts.CONTENT_URI, contactId);
Uri photoUri = Uri.withAppendedPath(person, Contacts.Photo.CONTENT_DIRECTORY);

Is there a way to do the same for a RawContact?

I've tried:

Uri person = ContentUris.withAppendedId(RawContacts.CONTENT_URI, rawContactId);
Uri photoUri = Uri.withAppendedPath(person, Contacts.Photo.CONTENT_DIRECTORY);

But it doesn't work...

The reason I need a URI instead of the actual blob is because the code is in an AppWidget and seems like there a very hard limit of a couple of megs when passing data from a widget to the launcher, so I need to use setImageViewUri and not setImageViewBitmap.

Thanks.


Solution

  • Here is the solution to your problem. Solution

    I also had the very same problem and after digging around and reading Android's source code I got it. It's probably to late by now (for you) but here it is anyway.

    public InputStream getBitmapFromUri(String rawContactId) throws FileNotFoundException {
    
        final Uri photoUri = Uri.withAppendedPath(
                ContentUris.withAppendedId(RawContacts.CONTENT_URI, Long.valueOf(rawContactId)), RawContacts.DisplayPhoto.CONTENT_DIRECTORY);
        final InputStream imageStream = contentResolver.openInputStream(photoUri);
    
        return imageStream;
    }