简体   繁体   English

如何获取Gmail帐户的个人资料图片

[英]How to get profile picture of gmail account

I am trying to get profile picture associated with gmail account on android. 我想在android上获取与gmail帐户相关的个人资料图片。 I have used account picker intent to select/login to gmail (authentication) in my app. 我已经使用帐户选择器意图在我的应用程序中选择/登录gmail(身份验证)。 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 在启动oAuth后,我最终得到以下结果方法

  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. 我不知道如何在上述案例中检索到个人资料图片的URL,任何帮助将不胜感激。

Quoting answer from - https://developers.google.com/identity/sign-in/android/people . 引用答案 - 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. 使用GoogleSignInResult.getSignInAccount方法为当前登录的用户请求个人资料信息。

You can call the getSignInAccount method after the sign-in intent succeeds. 您可以在登录意图成功后调用getSignInAccount方法。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 android 应用程序中获取 google 帐户资料图片 - How to get google account profile picture in android app 我怎样才能收到正确的 url? 尝试从 firebase 应用程序中的 googlesignin 帐户获取个人资料图片 - How can I receive the correct url ? Trying to get the profile picture from the googlesignin account in firebase app 当用户使用其Facebook帐户登录时,如何获取用户的姓名,电子邮件和个人资料图片? - How do I get the user's name, email, and profile picture when the user logs in with their Facebook account? 如何使用Java从Gmail帐户自动获取新邮件? - How to automatically get new mails from gmail account using java? 从Facebook获取个人资料图片并在imageview中设置 - get profile picture from facebook and set in imageview 使用userID获取大型个人资料图片Facebook - Using the userID to get a large profile picture Facebook 如何将图片从imageview发送到gmail? - How to send the picture from imageview to gmail? 获得带有GAE / java的gmail用户帐户? - get gmail user account with GAE/java? Android:如何验证我的Google帐户以及如何获取我的GMail任务? - Android: how do I authenticate to my google account and how can I get my GMail Tasks? 如何在Android中将个人头像图片放置在该位置? - How to place profile picture image on that place in Android?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM