Skip to content Skip to sidebar Skip to footer

How To Get Facebook User Profile (id,name,email And Picture) And Use In Other Activity By Sharedpreferences

I have an app that uses Facebook for login and then, go to another activity. First of all, I check the correct login and works fine. Then, I saved the dates of my profile on a Arra

Solution 1:

Facebook login :

publicvoidsetFacebookConnection() {

        LoginManager.getInstance().logOut();


        List<String> permissionNeeds = Arrays.asList("public_profile, email");

        LoginManager.getInstance().logInWithReadPermissions(mActivity, permissionNeeds);

        FacebookSdk.sdkInitialize(mActivity);

        callbackManager = CallbackManager.Factory.create();

        LoginManager.getInstance().registerCallback(callbackManager, newFacebookCallback<LoginResult>() {


            @OverridepublicvoidonSuccess(LoginResult loginResult) {

                GraphRequest request = GraphRequest.newMeRequest(loginResult.getAccessToken(), newGraphRequest.GraphJSONObjectCallback() {

                    @OverridepublicvoidonCompleted(JSONObject object, GraphResponse response) {

                        Log.d("res", object.toString());
                        Log.d("res_obj", response.toString());
                        try {

                            String id = object.getString("id");
                            try {
                                URL profile_pic = newURL("https://graph.facebook.com/" + id + "/picture?width=200&height=150");
                                Log.i("profile_pic", profile_pic + "");

                                String f_name = object.getString("first_name");
                                String l_name = object.getString("last_name");
                                String name = f_name + " " + l_name;
                                String email = object.getString("email");
                                String image = profile_pic.toString();


                                Log.d("data", email + name + image);
                                Stringtype = "facebook";

                                if (email == null) {

                                }
                            } catch (MalformedURLException e) {
                                e.printStackTrace();
                            }


                        } catch (JSONException e) {

                            e.printStackTrace();

                        }

                    }
                });
                Bundle parameters = newBundle();
                parameters.putString("fields", "id, first_name, last_name, email,gender");
                request.setParameters(parameters);
                request.executeAsync();
            }

            @OverridepublicvoidonCancel() {

                Log.d("fb_exception", "cancel by user");
            }

            @OverridepublicvoidonError(FacebookException exception) {

                Log.d("fb_exception", exception.toString());

            }
        });

    }

And onActivityResult method :

@OverrideprotectedvoidonActivityResult(int requestCode, int resultCode, Intent data) {

        callbackManager.onActivityResult(requestCode, resultCode, data);

        super.onActivityResult(requestCode, resultCode, data);
    }

To transfer data from one activity to other can use Shared preferences. As this login credentials will be needed through the application. so you can store them in shared preferences.

Try this:

  NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);

     headerView = navigationView.getHeaderView(0);

     tvUsername = (Textview) headerView.findViewById(R.id.username_textview_id);

     profile_image = (Textview) headerView.findViewById(R.id.profile_image);

This way you will get the references of imageview and textview in the left menu on top (in header view). ( Ref : image login register buttons )

To load the image can use Glide

Also verify your facebook login with how to implement facebook login in android using facebook sdk 4.7

Solution 2:

Try this code..

/**
 * this method used to user login with facebook.
 */privatevoidloginWithFacebook() {

    mLoginFbButton.setReadPermissions("public_profile", "email");
    mLoginFbButton.setFragment(this);
    mLoginFbButton.registerCallback(callbackManager, newFacebookCallback<LoginResult>() {
        @OverridepublicvoidonSuccess(LoginResult loginResult) {
            // App codeif (Profile.getCurrentProfile() == null) {
                mProfileTracker = newProfileTracker() {
                    @OverrideprotectedvoidonCurrentProfileChanged(Profile profile, Profile profile2) {
                 // here getting fb user profile object.       
                }
                };
            } else {
                registerUsingFaceBook(Profile.getCurrentProfile());
            }

        }

        @OverridepublicvoidonCancel() {

         }

        @OverridepublicvoidonError(FacebookException exception) {
        }
    });
}

Post a Comment for "How To Get Facebook User Profile (id,name,email And Picture) And Use In Other Activity By Sharedpreferences"