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

百般思念 提交于 2019-12-01 13:16:48

问题


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.


回答1:


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



回答2:


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.



来源:https://stackoverflow.com/questions/22681196/getting-null-bitmap-while-trying-to-get-facebook-image-using-facebook-id

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!