简体   繁体   中英

Android, Facebook SDK get profile picture fail

I am trying to get profile picture with user id using Facebook SDK.

But it is not working and I can't find any errors:

public class MainActivity extends AppCompatActivity {

    private TextView info;
    private LoginButton loginButton;
    private CallbackManager callbackManager;
    public String user_id;
    public ImageView imageView;
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        callbackManager.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        FacebookSdk.sdkInitialize(getApplicationContext());
        callbackManager = CallbackManager.Factory.create();
        setContentView(R.layout.activity_main);
        info = (TextView)findViewById(R.id.info);
        imageView =(ImageView)findViewById(R.id.profile_image);
        loginButton = (LoginButton)findViewById(R.id.login_button);

        loginButton.setReadPermissions(Arrays.asList("public_profile, email"));

        loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
            @Override
            public void onSuccess(LoginResult loginResult) {
                GraphRequest request = GraphRequest.newMeRequest(
                        loginResult.getAccessToken(),
                        new GraphRequest.GraphJSONObjectCallback() {
                            @Override
                            public void onCompleted(
                                    JSONObject object,
                                    GraphResponse response) {

                                try {
                                    user_id = response.getJSONObject().get("id") + "";
                                    Log.i("User id", user_id);
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }

                                try {
                                    response.getJSONObject().get("id");
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }

                            }
                        });
                Bundle parameters = new Bundle();
                parameters.putString("fields", "id,name,email,gender, birthday");
                request.setParameters(parameters);
                request.executeAsync();
                setProfile();
            }


            @Override
            public void onCancel() {
                info.setText("Login attempt canceled.");
            }

            @Override
            public void onError(FacebookException error) {
                info.setText("Login attempt failed.");
            }
        });

    }
    public void setProfile(){

        getFacebookProfilePicture(user_id);

    }

    public  void getFacebookProfilePicture(String userID){

        try {



            String imageUrl = "https://graph.facebook.com/" + userID + "/picture?type=large";

            Picasso.with(getApplicationContext()).load(imageUrl).into(imageView);
             //bitmap = BitmapFactory.decodeStream((InputStream)imageUrl.getContent());

            //InputStream is = (InputStream) imageUrl.getContent();
            //Drawable d = Drawable.createFromStream(is, "src name");

        }
        catch (Exception e){
            Log.e("ReadRdaJSONFeedTask", e.getLocalizedMessage() == null ? "ERROR IS NULL" : "ERROR IS NOT NULL AND IT IS:"+e.getLocalizedMessage());
        }

    }
}

put setProfile(); below

user_id = response.getJSONObject().get("id") + "";
Log.i("User id", user_id);

inside

 @Override
 public void onCompleted(JSONObject object, GraphResponse response) { });

so your code should be

    loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
            @Override
            public void onSuccess(LoginResult loginResult) {
                GraphRequest request = GraphRequest.newMeRequest(
                        loginResult.getAccessToken(),
                        new GraphRequest.GraphJSONObjectCallback() {
                            @Override
                            public void onCompleted(
                                    JSONObject object,
                                    GraphResponse response) {

                                try {
                                    user_id = response.getJSONObject().get("id") + "";
                                    Log.i("User id", user_id);
                                    setProfile();
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }

                                try {
                                    response.getJSONObject().get("id");
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }

                            }
                        });
                Bundle parameters = new Bundle();
                parameters.putString("fields", "id,name,email,gender, birthday");
                request.setParameters(parameters);
                request.executeAsync();

            }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM