Get contacts photo which are synced with facebook for android

后端 未结 5 726
囚心锁ツ
囚心锁ツ 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:31

    There is definitely no way to do this in a standard way. So, we must use an SQL Injection in order to be able to hack the contacts database and get the Facebook avatars. The following code works on most Motorolas, which use Motoblur, on Android 2.2 or higher:

    public static Bitmap loadFacebookAvatar(Context context, long personId) {
        String[] rawProjection = {ContactsContract.RawContacts._ID};
        String contactIdAssertion = ContactsContract.RawContacts.CONTACT_ID + " = " + personId;
        String rawWhere = new StringBuilder()
                .append(contactIdAssertion).append(") UNION ALL SELECT ")
                .append(ContactsContract.RawContacts._ID).append(" FROM view_raw_contacts WHERE (")
                .append(contactIdAssertion).toString();
        Cursor query = context.getContentResolver().query(ContactsContract.RawContacts.CONTENT_URI,
                rawProjection,
                rawWhere, null, null);
        if (query != null && query.moveToFirst()) {
            do {
                long id = query.getLong(query.getColumnIndex(ContactsContract.RawContacts._ID));
                String[] projection = {ContactsContract.CommonDataKinds.Photo.PHOTO};
                Uri uri = ContactsContract.Data.CONTENT_URI;
    
                String mimeTypeAssertion = ContactsContract.Data.MIMETYPE + "='" + ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE + "'";
                String photoAssertion = ContactsContract.CommonDataKinds.Photo.PHOTO + " IS NOT NULL";
                String rawContactIdAssertion = ContactsContract.CommonDataKinds.Photo.RAW_CONTACT_ID + " = " + id;
    
                String where = new StringBuilder().append(mimeTypeAssertion).append(" AND ")
                        .append(photoAssertion).append(" AND ").append(rawContactIdAssertion)
                        .append(") UNION ALL SELECT ").append(ContactsContract.CommonDataKinds.Photo.PHOTO)
                        .append(" FROM view_data WHERE (").append(photoAssertion).append(" AND ")
                        .append(rawContactIdAssertion).toString();
    
                Cursor photoQuery = context.getContentResolver().query(uri, projection, where, null, null);
                if (photoQuery != null && photoQuery.moveToFirst()) {
                    do {
                        byte[] photoData = photoQuery.getBlob(photoQuery.getColumnIndex(ContactsContract.CommonDataKinds.Photo.PHOTO));
                        if (photoData != null) {
                            return BitmapFactory.decodeByteArray(photoData, 0, photoData.length, null);
                        }
                    } while (photoQuery.moveToNext());
                }
            } while (query.moveToNext());
        }
        return null;
    }
    

    For other handsets you must get the contacts database and analyze it in order to determine how to apply the SQL Injection, which requires a rooted phone.

提交回复
热议问题