views:

68

answers:

1

Hello stack people,

I am trying to show contact pictures in my application but I am getting pictures of those who were added manually only and not those which are synced with facebook. How to work around this? Here is my code below:

            Uri uri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, Long.parseLong(PhotoId));
            InputStream input = ContactsContract.Contacts.openContactPhotoInputStream(context.getContentResolver(), uri);
            return BitmapFactory.decodeStream(input);
A: 

The code you have given should only access the default photo. Also, you should be appending the contact ID to that URI, not the photo ID (assuming you're using the photo's id from the data table).

If there are multiple photos you might want to try accessing them directly from the Data table. You'll need to parse a database cursor and convert the raw byte data into a bitmap manually as shown below:

String[] projection = {ContactsContract.CommonDataKinds.Photo.PHOTO};
Uri uri = Uri. ContactsContract.Data.CONTENT_URI;
String where = ContactsContract.Data.MIMETYPE 
       + "=" + ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE + " AND " 
       + ContactsContract.Data.CONTACT_ID + " = " + mContactId;
Cursor cursor = context.getContentResolver().query(uri, projection, null, null, null);

if(cursor!=null&&cursor.moveToFirst()){
    do{
        byte[] photoData = photoCursor.getBlob(0);
        Bitmap photo = BitmapFactory.decodeByteArray(photoData, 0,
                photoData.length, null);

        //Do whatever with your photo here...
    }while(cursor.moveToNext());
}

You would want mContactId to correspond with the contact that you want photos for.

If you want to limit to only facebook photos, you'll need to use ContactsContract.Data.RAW_CONTACT_ID instead, which you should get from the RawContacts table using your contact Id and a filter based on the facebook account (assuming you know what account you're looking for... that can vary by sync provider implementation...)

Marloke
This method still results in a null byte for pictures from facebook for android. I guess it is because the api of facebook for android has a whitelist system which allows data to be shared to only android default applications.
Yash
Still thank you for your response
Yash
I could have sworn that I've loaded facebook photos before using a similar method... it may depend on your sync provider though. I believe there are several different ones out there. Have you tried switching to a different facebook client app?
Marloke
Yeah that is the exact problem. The official app for facebook (facebook for android) seems to have a stupid policy that prevents other apps from using the data. I am pretty sure that another sync app for facebook will do the job.
Yash