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

前端 未结 2 1391
误落风尘
误落风尘 2021-01-17 04:00

This is the code I\'m using.

String imageURL;
    Bitmap bitmap = null;

    imageURL = \"http://graph.facebook.com/\" + fbID + \"/picture?type=\";
    try {         


        
相关标签:
2条回答
  • 2021-01-17 04:29

    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();
    
    0 讨论(0)
  • 2021-01-17 04:33

    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.

    0 讨论(0)
提交回复
热议问题