0

This is the code I'm using.

String imageURL;
    Bitmap bitmap = null;

    imageURL = "http://graph.facebook.com/" + fbID + "/picture?type=";
    try {
        bitmap = BitmapFactory.decodeStream((InputStream) new URL(imageURL)
                .getContent());

    } catch (Exception e) {
        e.printStackTrace();
    }

when i use this URL in browser it shows image, but when trying to get it in bitmap it gives null.

I've checked many question, but nothing help me.

Ankit
  • 483
  • 7
  • 24

2 Answers2

5

This code works for me with HTTPGet:

                HttpGet httpRequest = new HttpGet(URI.create(linkUrl) );
                HttpClient httpclient = new DefaultHttpClient();
                HttpResponse response = (HttpResponse) httpclient.execute(httpRequest);
                HttpEntity entity = response.getEntity();
                BufferedHttpEntity bufHttpEntity = new BufferedHttpEntity(entity);
                bmap = BitmapFactory.decodeStream(bufHttpEntity.getContent());
                httpRequest.abort();
fr4n
  • 755
  • 1
  • 8
  • 16
1

This problem is due to the fact that, Facebook uses its graph API url merely as a redirection endpoint. If you copy paste a facebook profile picture URL such as

http://graph.facebook.com/subinsebastien/picture

What actually happens is that, Facebook redirects you to the actual image URL. Thus, when you try to open an input stream with the graph API url, you wont actually get a bitmap, rather a redirect response is what you get. In my case, this is where I'm being redirected to.

https://scontent-b.xx.fbcdn.net/hprofile-prn1/l/t5.0-1/48771_100000333400619_475851971_q.jpg

Thus, I would rather suggest you to use an actual HTTPGet to get the bitmap first.

C--
  • 16,393
  • 6
  • 53
  • 60
  • Yes, you r right. when i'm using this url (statically) it gives the result. https://scontent-b.xx.fbcdn.net/hprofile-prn1/l/t5.0-1/48771_100000333400619_475851971_q.jpg – Ankit Mar 27 '14 at 08:26
  • do you have any suggestion that how would i use HTTPGet. – Ankit Mar 27 '14 at 08:27