Search code examples
androidfacebookauthenticationpicassouser-profile

Using Picasso to get Facebook profile picture at login


I have searched the internet high and low and cannot find a solution to this problem that works for me. When the user signs into the app using the facebook login button, I want to get their Facebook profile picture and use it as their profile picture in my app. I'm trying to use Picasso to get the picture from the Facebook URL. Here is my code as it is. I'm getting an error

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);

    if (getIntent().getBooleanExtra("EXIT", false)) {
    }

    FacebookSdk.sdkInitialize(getApplicationContext());
    AppEventsLogger.activateApp(this);

    loginButton = (LoginButton) findViewById(R.id.fb_login_btn);
    callbackManager = CallbackManager.Factory.create();


    accessTokenTracker = new AccessTokenTracker() {

        @Override
        protected void onCurrentAccessTokenChanged(AccessToken 
    oldAccessToken, AccessToken currentAccessToken) {

        }
    };

    accessToken = AccessToken.getCurrentAccessToken();

    if (AccessToken.getCurrentAccessToken() == null) {

        loginButton.registerCallback(callbackManager, new 
        FacebookCallback<LoginResult>() {
            @Override
            public void onSuccess(LoginResult loginResult) {

                Picasso.with(this) //I'm getting an error here "Picasso 
                cannot be applied"
                        .load("https://graph.facebook.com/" + 
                        loginResult.getAccessToken().getUserId(); + 
                        "/picture?type=large")
                        .into(profilePhoto);



                Toast.makeText(LoginActivity.this, "Login Successful", 
                Toast.LENGTH_SHORT).show();
                {
                    Intent intent = new Intent(LoginActivity.this, 
                    MainActivity.class);
                    startActivity(intent);
                }

                finish();

            }

            @Override
            public void onCancel() {

                Toast.makeText(LoginActivity.this, "Login Cancelled", 
                Toast.LENGTH_SHORT).show();

            }

            @Override
            public void onError(FacebookException error) {

                Toast.makeText(LoginActivity.this, "Login Error", 
                Toast.LENGTH_SHORT).show();

            }
        });



    } else {

        Intent intent = new Intent(LoginActivity.this, 
        MainActivity.class);
        startActivity(intent);
        this.finish();

    }
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent 
data) {
    callbackManager.onActivityResult(requestCode,resultCode,data);
}

@Override
public void onDestroy() {
    super.onDestroy();
    accessTokenTracker.stopTracking();
}
}

Solution

  • The problem is with this part

     Picasso.with(this) //I'm getting an error here "Picasso 
                cannot be applied"
                        .load("https://graph.facebook.com/" + 
                        facebook_id + "/picture?type=large")
                        .into(profilePhoto);
    

    Here instead of 'this' you need to pass activity or application context. If you are in activity then type 'YourActivityName.this' or if you are in fragment then use 'getActivity'. If you are thinking why 'this' is not working then FYI 'this' here means anonymous inner class.