15

I am using Facebook sdk 4.4.0 in android and I want to get current profile picture of the user using graph request. How to do that?

I have seen that people use

https://graph.facebook.com/me/picture?access_token=ACCESS_TOKEN

API to extract profile picture but I cant figure how to extract profile picture from it.

Rajesh Jadav
  • 12,801
  • 5
  • 53
  • 78
anupam x
  • 779
  • 2
  • 7
  • 12
  • For getting picture from `GraphRequest` there can be certain params you can pass. All Documented on https://developers.facebook.com/docs/graph-api/reference/user/picture/. – ADM Jul 24 '18 at 03:55

9 Answers9

63

You need to call GraphRequest API for getting all the details of user in which API also gives URL of current profile picture.

Bundle params = new Bundle();
params.putString("fields", "id,email,gender,cover,picture.type(large)");
new GraphRequest(AccessToken.getCurrentAccessToken(), "me", params, HttpMethod.GET,
        new GraphRequest.Callback() {
            @Override
            public void onCompleted(GraphResponse response) {
                if (response != null) {
                    try {
                        JSONObject data = response.getJSONObject();
                        if (data.has("picture")) {
                            String profilePicUrl = data.getJSONObject("picture").getJSONObject("data").getString("url");
                            Bitmap profilePic= BitmapFactory.decodeStream(profilePicUrl .openConnection().getInputStream());
                            mImageView.setBitmap(profilePic);
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
}).executeAsync();
Rajesh Jadav
  • 12,801
  • 5
  • 53
  • 78
  • This throws errors for me because decodeStream is looking for a URL instead of a string - Am I doing something wrong? – ntgCleaner Feb 11 '16 at 22:04
  • have you try BitmapFactory.decodeStream(url.openConnection().getInputStream()); ?? – Rajesh Jadav Feb 12 '16 at 03:53
  • 1
    I have used your exact text there, placed the `getFacebookProfilePicture()` outside of onCreate and It's showing `.openConnection()` as not resolved. Not sure what I'm doing wrong – ntgCleaner Feb 12 '16 at 14:26
  • @ntgCleaner i have updated code please use updated getFacebookProfilePicture method – Rajesh Jadav Feb 12 '16 at 14:41
  • 1
    Beautiful! It works perfectly now. Instead of `token`, I used `AccessToken.getCurrentAccessToken()` (as per facebook API). It works great! Thank you for updating an older answer! – ntgCleaner Feb 12 '16 at 14:51
  • 4
    Wow! Thank you, FabCoder! For these few words "picture.type(large)". I cannot find this opportunity in the facebook's documentation, but I knew that this is exists! Thank you! – Sirelon Apr 19 '16 at 11:45
  • Error at .openConnection() is remove by converting String (profilePicUrl) into URL. – shehzy Sep 21 '17 at 07:00
17

From last sdk 4.5.0

 String url;
 Bundle parametersPicture = new Bundle();
 parametersPicture.putString("fields", "picture.width(150).height(150)");

 GraphResponse lResponsePicture = new GraphRequest(AccessToken.getCurrentAccessToken(), "/me/",
                        parametersPicture, null).executeAndWait();
 if (lResponsePicture != null && lResponsePicture.getError() == null &&
                            lResponsePicture.getJSONObject() != null) {
     url = lResponsePicture.getJSONObject().getJSONObject("picture")
                                .getJSONObject("data").getString("url");
 }
Dmitriy Puchkov
  • 1,530
  • 17
  • 41
4

Get Image From Facebook

String image_url = "http://graph.facebook.com/" + Profile.getCurrentProfile().getId() + "/picture?type=large";
Glide.with(activity)
     .load(image_url)
     .into(imageView);

dependency

implementation 'com.github.bumptech.glide:glide:4.1.1'
Ahamadullah Saikat
  • 4,437
  • 42
  • 39
3

You can have this in 2 different way.

Way1: Integrate graph api support https://developers.facebook.com/docs/graph-api/reference/user/picture/

Way2: Via Get Call http:// graph.facebook.com/{facebook-Id}/picture?width=x&height=y

where x and y could be any integer e.g. 100

Faizan Tariq
  • 351
  • 4
  • 14
3

If you want really big picture you would have to spec. at least one size of picture - for ex.

String profileImg = "https://graph.facebook.com/" + loginResult.getAccessToken().getUserId() + "/picture?type=large&width=1080";

Also, you can specify both sizes (add &height=some_val), but then facebook will crop this profile image.

Anton Kizema
  • 1,072
  • 3
  • 13
  • 27
3
protected void rajendra(LoginButton login_button) {
    login_button.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
        @Override
        public void onSuccess(LoginResult login_result) {
            GraphRequest request = GraphRequest.newMeRequest(
                    login_result.getAccessToken(),
                    new GraphRequest.GraphJSONObjectCallback() {
                        @Override
                        public void onCompleted(JSONObject object,GraphResponse response) {
                       
                            response.getError();

                            try {
                                if (android.os.Build.VERSION.SDK_INT > 9) {
                                    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
                                    StrictMode.setThreadPolicy(policy);
                                    String profilePicUrl = object.getJSONObject("picture").getJSONObject("data").getString("url");
                               
                                    URL fb_url = new URL(profilePicUrl);//small | noraml | large
                                    HttpsURLConnection conn1 = (HttpsURLConnection) fb_url.openConnection();
                                    HttpsURLConnection.setFollowRedirects(true);
                                    conn1.setInstanceFollowRedirects(true);
                                    Bitmap fb_img = BitmapFactory.decodeStream(conn1.getInputStream());
                                    image.setImageBitmap(fb_img);
                                }
                            }catch (Exception ex) {
                                ex.printStackTrace();
                            }
                        }
                    });
            Bundle parameters = new Bundle();
            parameters.putString("fields", "id,picture");
            request.setParameters(parameters);
            request.executeAsync();
        }
 }
Dharmishtha
  • 1,313
  • 10
  • 21
rajendra
  • 41
  • 1
2
try {
String fbId="970463683015249";
URL fb_url = new URL("http://graph.facebook.com/"+fbId+"/picture?type=small");//small | noraml | large
HttpsURLConnection conn1 = (HttpsURLConnection) fb_url.openConnection();
HttpsURLConnection.setFollowRedirects(true);
conn1.setInstanceFollowRedirects(true);
Bitmap fb_img = BitmapFactory.decodeStream(conn1.getInputStream());
}catch (Exception ex) {
  ex.printStackTrace();
  }
Premkumar Manipillai
  • 2,121
  • 23
  • 24
0

Simply call this URL:

graph.facebook.com/<facebook_user_id>/picture?type=large

type can be large, normal or small.

Another way is to use ProfilePictureView

<com.facebook.login.widget.ProfilePictureView
    android:id="@+id/image"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    facebook:preset_size="small"/>

After that you can set facebook id like this in code

profilePictureView.setProfileId(facebookUserId);
Malwinder Singh
  • 6,644
  • 14
  • 65
  • 103
  • thanks this works. Can you tell me how to use this link with graph request api? a small code snippet – anupam x Aug 31 '15 at 12:22
0

facebook image

https://graph.facebook.com/" +facebookUid + "/picture?height=9999&redirect=0"

google image

String googleEmailName = googleEmail.substring(0,googleEmailName.indexOf("@"))

http://picasaweb.google.com/data/entry/api/user/"+googleEmailName+"?alt=json

HongSec Park
  • 1,193
  • 8
  • 9