Ankit
Ankit

Reputation: 491

Getting null bitmap while trying to get Facebook image using Facebook ID

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.

Upvotes: 0

Views: 971

Answers (2)

fr4n
fr4n

Reputation: 755

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();

Upvotes: 5

C--
C--

Reputation: 16558

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.

Upvotes: 1

Related Questions