Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get contacts photo which are synced with facebook for android

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);
like image 919
Yash Avatar asked Oct 02 '10 11:10

Yash


2 Answers

It doesn't work for contacts that are synced from FB only. You'll need to use the FB graph API and fetch the photo from there; and you need to know the contacts facebook name.

 Bitmap contactPhoto = getImageBitmap("http://graph.facebook.com/mathiaslin/picture?type=square");

 final private static Bitmap getImageBitmap(String url) {
    Bitmap bm = null;
    try {
        URLConnection conn = new URL(url).openConnection();
        conn.connect();
        InputStream is = conn.getInputStream();
        BufferedInputStream bis = new BufferedInputStream(is);
        bm = BitmapFactory.decodeStream(bis);
        bis.close();
        is.close();
    } catch (IOException e) {
        Log.e(TAG, "Error getting bitmap", e);
    }
    return bm;
}
like image 161
Mathias Conradt Avatar answered Oct 01 '22 04:10

Mathias Conradt


I have tried every solution found on Stack Overflow at the time of writing this and nothing will correctly retrieve a photo that came from Facebook through the official Facebook app account sync adapter.

To be clear, people posting solutions who "think" they work are probably using HTC sense phones that come with a Facebook sync adapter written by HTC that doesn't have the same security requirements as the official Facebook app.

It's a security thing and one of the methods did manage to try to access the bytes of the facebook photo and you will end up with an SqliteException saying that the content requested is restricted.

Same code run as a system process will pull the photo fine. It is just not possible as of right now.

like image 20
Eric Avatar answered Oct 01 '22 03:10

Eric