Android : Getting a Bitmap from a url connection (graph.facebook.com) that redirects to another url

前端 未结 2 1953
情书的邮戳
情书的邮戳 2021-01-15 15:59

I have done everything to get a url where i can get the profile pic of a facebook user.

The only problem left now is to get that image into a bitmap object.

2条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-15 16:43

    You are right in saying that http://graph.facebook.com redirects the connection first (as we can see in the url) to https://fbcdn-profile-a.akamaihd.net/, but-

    auto redirection works automatically when original and redirected protocols are same.

    So, if you try to load images from https instead of http : "https://graph.facebook.com/USER_ID/picture"; since image's url is "https://fbcdn-profile-a.akamaihd.net/....", BitmapFactory.decodeStream shall work again to get you the bitmap.

    Here's the code-

    URL imgUrl = new URL("https://graph.facebook.com/{user-id}/picture?type=large");
    InputStream in = (InputStream) imgUrl.getContent();
    Bitmap  bitmap = BitmapFactory.decodeStream(in);
    

    Hope that helps. Good luck.

提交回复
热议问题