试图从登录的个人资料中获取照片。但总是返回null。姓名和电子邮件返回值,只有照片有问题。
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestProfile()
.requestEmail()
.build();
mGoogleApiClient = new GoogleApiClient.Builder(StartActivity.this)
.enableAutoManage(StartActivity.this, StartActivity.this)
.addApi(Auth.GOOGLE_SIGN_IN_API, gso)
.build();
acct = gResult.getSignInAccount();
String name = acct.getDisplayName(); //okay, value != null
String email = acct.getEmail(); //okay, value != null
Uri photoUri = acct.getPhotoUrl() //not okay, value == null
为什么会这样?帐户已签名,电子邮件和姓名已获得,但照片始终失败。
根据Google's documentation - GoogleSignInAccount
public Uri getPhotoUrl()
获取已登录用户的照片网址。
返回
Google帐户的照片网址。如果配置了requestProfile()且用户确实拥有Google+个人资料图片,则仅为非null。
请检查您的Google帐户是否有Google+个人资料图片。
P / S:有时,如果已经创建了Google+个人资料图片,但是在您的设备中添加Google帐户之后,您可能需要从设备中删除现有的Google帐户,然后重新添加。
如果acct.getPhotoUrl()返回null
表示您的Google帐户未与Google Plus相关联..如果您拥有正确的Google Plus帐户,则不会为空
解:
将有机会获得null ...解决方案如下
有一个休息网络服务,您将通过它获取您的图像网址
https://www.googleapis.com/plus/v1/people/ {ID}?字段=图像&关键= {} API_KEY
“id”你将通过acct.getId()方法调用和API_KEY获得你将从你的开发者控制台获得...确保API_KEY是浏览器密钥....不是android或服务器密钥....
在开发者控制台中启用Google+ Plus api ...
现在,您可以通过以上服务电话获取您的个人资料图片
https://www.googleapis.com/plus/v1/people/ {ID}?字段=图像&关键= {} API_KEY
你会得到如下的回复
{“image”:{“url”:“https://lh4.googleusercontent.com/-StnGV_eLi3s/AAAAAAAAAAI/AAAAAAAABHU/XLH5wQ_Rm9E/photo.jpg?sz=50”,“isDefault”:false}}
现在你把你的网址作为imageUrl ....
现在微笑请继续编码!
面临同样的问题,即将photoURL和tokenID视为null
正如@BNK已经解释了其他原因可能是什么
因此,只需将我的解决方案添加到其中,对于面临同样问题的人(希望,这有助于他们)
使用下面的代码(用kotlin编写)
val gso = GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestEmail()
.build()
我只能获取“DisplayName”,“电子邮件”,“FamilyName”,“ID”等信息,但photoUrl和tokenId除外
所以我刚刚向GSO Builder添加了'requestIdToken',如下所示:
val gso = GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestEmail()
.requestIdToken(resources.getString(R.string.googleAccountWebClientID)) // This line did the magic for me
.build()
因此,我可以获取所有信息以及'idToken'和'photoUrl'
注意:请将'WebClientId'用于requestIdToken(),您可以从中获取(https://console.developers.google.com/apis/credentials?project=)
尝试这个代码我这是你的问题的工作。
String _name;
@Override
public void onConnected(Bundle arg0) {
Log.e(TAG, "onConnected");
Plus.PeopleApi.loadVisible(mGoogleApiClient, null).setResultCallback(this);
Person currentUser = Plus.PeopleApi.getCurrentPerson(mGoogleApiClient);
Log.e("USERNAME_Con",Plus.AccountApi.getAccountName(mGoogleApiClient));
Log.e("USERNAME_Con",currentUser.getBirthday()+" "+currentUser.getImage().getUrl()+" "+currentUser.getId());
String personPhotoUrl=currentUser.getImage().getUrl();
if(currentUser!=null){
String email=Plus.AccountApi.getAccountName(mGoogleApiClient);
String id=currentUser.getId();
_name=currentUser.getDisplayName();
progressDialog = ProgressDialog.show(Form.this, "", "Loading...", true,false);
logingoogle(email,id,_name);
}
personPhotoUrl = personPhotoUrl.substring(0,
personPhotoUrl.length() - 2)
+ 400;
Log.e("USER Image",""+personPhotoUrl);
new LoadProfileImage().execute(personPhotoUrl);
// Indicate that the sign in process is complete.
mSignInProgress = STATE_DEFAULT;
}
private class LoadProfileImage extends AsyncTask<String, Void, Bitmap> {
protected Bitmap doInBackground(String... urls) {
String urldisplay = urls[0];
Bitmap mIcon11 = null;
try {
InputStream in = new URL(urldisplay).openStream();
mIcon11 = BitmapFactory.decodeStream(in);
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
camera=mIcon11;
return mIcon11;
}
protected void onPostExecute(Bitmap result) {
// bmImage.setImageBitmap(result);
setimage(camera);
}
}
if(acct.getPhotoUrl() == null){
//set default image
} else {
photo_url = acct.getPhotoUrl().toString(); //photo_url is String
}
如果可能的话,只有你在build.gradle中使用这个版本的play服务进行编译,用这个改变你的旧实现:
implementation 'com.google.android.gms:play-services-auth:12.0.1'
之后,您可以对活动结果执行此操作:
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RC_SIGN_IN) {
GoogleSignInResult result =Auth.GoogleSignInApi.getSignInResultFromIntent(data);
handleSignInResult(result);
}
}
及以下:
private void handleSignInResult(GoogleSignInResult result) {
GoogleSignInAccount acct = result.getSignInAccount();
email = acct.getEmail();
first_name = acct.getDisplayName();
pic_profile = acct.getPhotoUrl().toString();
}
适合我!
当我在我的Android应用程序中发生这种情况时,我将该帐户从设备上删除并将其取回;它起作用了。最初我在帐户中没有照片。然后我加了一个。这是一个运行Jellybean的设备。