Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get profile picture of gmail account

I am trying to get profile picture associated with gmail account on android. I have used account picker intent to select/login to gmail (authentication) in my app. I have used following code

 mCredential = GoogleAccountCredential.usingOAuth2(
 getApplicationContext(), Arrays.asList(SCOPES))
 .setBackOff(new ExponentialBackOff())
 .setSelectedAccountName(settings.getString("PREF_ACCOUNT_NAME", null));

After initiating oAuth i end up with following method for result

  protected void onActivityResult(
        int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    switch(requestCode) {

        case REQUEST_ACCOUNT_PICKER:
            if (resultCode == RESULT_OK && data != null &&
                    data.getExtras() != null) {
                String accountName =
                        data.getStringExtra(AccountManager.KEY_ACCOUNT_NAME);



                            Log.e("ghfyg",""+accountName);
                if (accountName != null) {
                    mCredential.setSelectedAccountName(accountName);
                    SharedPreferences settings =
                            AccountSync.this.getSharedPreferences("deep",Context.MODE_PRIVATE);
                    SharedPreferences.Editor editor = settings.edit();
                    editor.putString("PREF_ACCOUNT_NAME", accountName);
                    editor.apply();
                }
            } else if (resultCode == RESULT_CANCELED) {
                Toast.makeText(this,"Account unspecified.",Toast.LENGTH_SHORT).show();
            }
            break;

    }

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

I am not sure how i can retrieve URL to profile picture in above case, any help will be appreciated.

like image 460
Deepmala singh M Avatar asked Oct 18 '22 03:10

Deepmala singh M


1 Answers

Quoting answer from - https://developers.google.com/identity/sign-in/android/people.

Retrieve profile information for a signed-in user

Use the GoogleSignInResult.getSignInAccount method to request profile information for the currently signed in user.

You can call the getSignInAccount method after the sign-in intent succeeds.

GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
GoogleSignInAccount acct = result.getSignInAccount();
String personName = acct.getDisplayName();
String personEmail = acct.getEmail();
String personId = acct.getId();
Uri personPhoto = acct.getPhotoUrl();
like image 73
theabhinavdas Avatar answered Oct 21 '22 04:10

theabhinavdas