Android - get facebook profile picture

后端 未结 28 2315
花落未央
花落未央 2020-12-12 15:13

I don\'t know why, but I am always getting null when I try to get the profile picture of the user. Do I need to set some specific permissions to get access?

Below is

相关标签:
28条回答
  • 2020-12-12 15:50

    With Glide:

    userId = loginResult.getAccessToken().getUserId();
    

    then;

    Glide.with(this)
            .load("https://graph.facebook.com/" + userId+ "/picture?type=large")
            .into(imgProfile);
    
    0 讨论(0)
  • 2020-12-12 15:50

    This should solve it. but be sure to access setfollowredirects statically i.e HttpURLConnection.setFollowRedirects(HttpURLConnection.getFollowRedirects());

    url = new URL("https://graph.facebook.com/ID/picture?type=small");
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();                                           HttpURLConnection.setFollowRedirects(HttpURLConnection.getFollowRedirects());
    connection.setDoInput(true);
    connection.connect();
    input = connection.getInputStream();
    
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeStream(input, null, options);
    
    // Calculate inSampleSize
    options.inSampleSize = calculateInSampleSize(options, 300, 300);
    
     // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;
    options.inPreferredConfig = Config.RGB_565;
    myBitmap= BitmapFactory.decodeStream(input, null, options);
    

    or

    url = new URL("https://graph.facebook.com/ID/picture?type=small");
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();                                                HttpURLConnection.setFollowRedirects(HttpURLConnection.getFollowRedirects());
    connection.setDoInput(true);
    connection.connect();
    input = connection.getInputStream();
    myBitmap= BitmapFactory.decodeStream(input);
    

    Hope this helps

    0 讨论(0)
  • 2020-12-12 15:52

    I always received a response stating a FACEBOOK_NON_JSON_RESULT. So looking back in Facebook’s graph API explorer I noted a little checkbox with the label redirect checked. Some googling showed me that I needed to provide a parameter to my GraphRequest that disallows the redirect. Hence the correct request must be:

     Bundle params = new Bundle();
     params.putBoolean("redirect", false);
    
         new GraphRequest(
         AccessToken.getCurrentAccessToken(),
         "me/picture",
         params,
         HttpMethod.GET,
         new GraphRequest.Callback() {
            public void onCompleted(GraphResponse response) {
                try {
                    String picUrlString = (String) response.getJSONObject().getJSONObject("data").get("url");   
                    //Load picture url in imageView
                    Glide.with(this).load(picUrlString).diskCacheStrategy(DiskCacheStrategy.SOURCE).into(profilePictureView);
                } catch (JSONException | IOException e) {
                    e.printStackTrace();
                }
            }
        }
     ).executeAsync();                                      
    
    0 讨论(0)
  • 2020-12-12 15:52

    When you make request like this:

    http://graph.facebook.com/103407310026838/picture?type=square&type=large
    

    it makes a redirect to other url..

    You need to add an extra param on Get request

    redirect=false
    

    Like this

    http://graph.facebook.com/103407310026838/picture?type=square&type=large&redirect=false
    

    and you will get the a Json with Real Image url..

    Like this :

    {
       "data": {
          "is_silhouette": true,
          "url": "https://scontent.xx.fbcdn.net/v/t1.0-1/s200x200/1379841_10150004552801901_469209496895221757_n.jpg?oh=4234dcdfc832a58b9ef7a31c7896c73c&oe=57DD01F8"
       }
    }
    

    finally make new request to get the image that you found in data->url

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