4

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.

marmor
  • 27,641
  • 11
  • 107
  • 150
  • Did you manage to get the RawContact photo? I'm also looking for a way to get it but to no success... – dazito Feb 19 '14 at 10:17

4 Answers4

2

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;
}
Community
  • 1
  • 1
dazito
  • 7,740
  • 15
  • 75
  • 117
  • Nice find!! :) I've read a bit on ContactsContract.RawContacts.DisplayPhoto, seems like they only added this on API 14, so that might explain why I never found it digging through the docs. – marmor Feb 20 '14 at 09:04
1

I think you need to directly access the 'data' table(ContactsContract.Data) to get the photo for raw contacts. The column 'data15' should store it. Contacts.Photo.CONTENT_DIRECTORY is the contact's primary photo path, but no such path is provided for raw contacts in default.

zlan
  • 31
  • 6
  • Thanks, but data15 is the photo blob itself, so I can't use setImageViewUri on it... can I somehow get the URI of that blob? – marmor Aug 29 '11 at 05:49
0

A year later and no real answer, I guess it can't be done.

I ended up using setImageBitmap instead of setImageUri, and used a scaling code for the bitmap to reduce memory consumption as much as possible (a loop that requests smaller and smaller sizes of the bitmap until the size is under a const limit).

this seemed to work well for almost all our users, some still complain about missing pics in the widget, but I'd say less then 2-3% of our users which is kinda acceptable.

marmor
  • 27,641
  • 11
  • 107
  • 150
0

If you are querying for RawContacts you can also add RawContacts.CONTACT_ID to the projection, then you can build the URI the same way:

long contactId = cursor.getLong(cursor.getColumnIndex(RawContacts.CONTACT_ID));
Uri person = ContentUris.withAppendedId(Contacts.CONTENT_URI, contactId);
Uri photoUri = Uri.withAppendedPath(person, Contacts.Photo.CONTENT_DIRECTORY);
Giohji
  • 714
  • 7
  • 12
  • Thanks @Guihji, but I need to access the RawContact's photo, not the selected primary for that contact. I need the raw's because on some devices Facebook synced photos aren't accessible to 3rd party apps, so I first try to get the primary, if I can't access it (means it's a FB photo), I try to get another photo from one of the Raw's. – marmor Oct 11 '11 at 13:58