How to get Facebook profile image in Android

拥有回忆 提交于 2019-11-27 01:23:14

问题


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.


回答1:


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



回答2:


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");
 }



回答3:


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'



回答4:


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




回答5:


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.




回答6:


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



回答7:


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



回答8:


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



回答9:


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



来源:https://stackoverflow.com/questions/32310878/how-to-get-facebook-profile-image-in-android

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