Get contacts photo which are synced with facebook for android

后端 未结 5 731
囚心锁ツ
囚心锁ツ 2020-12-25 09:56

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 w

5条回答
  •  眼角桃花
    2020-12-25 10:46

    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...)

提交回复
热议问题