[英]Android Firebase Auth - Get User's Photo
How can I retrieve a photo of the user with decent resolution that can be used from a mobile app? 如何从移动应用程序中检索具有相当分辨率的用户照片? I looked at the guides and the api docs and the recommended way seemed to be to use FirebaseUser#getPhotoUrl() . 我查看了指南和api文档,推荐的方法似乎是使用FirebaseUser#getPhotoUrl() 。 This however gives back a url to a photo with resolution 50x50 px, which is too low to be useful. 然而,这会给出分辨率为50x50像素的照片的网址,该照片太低而无法使用。 Is there a way for the client to request a higher res photo of the user? 有没有办法让客户要求更高的用户照片? I've tested the sdks of Facebook Login and Google Sign-in separately, and in both cases the resolutions of the photos are higher than what Firebase Auth gives back. 我已经分别测试了Facebook登录和Google登录的sdks,在这两种情况下,照片的分辨率都高于Firebase Auth的回复。 Why does Firebase Auth change the original resolutions and how can I force it not to do that? 为什么Firebase Auth会更改原始分辨率,如何强制它不要这样做? Thanks. 谢谢。
Facebook and Google PhotoURL : Facebook和Google PhotoURL:
User myUserDetails = new User();
myUserDetails.name = firebaseAuth.getCurrentUser().getDisplayName();
myUserDetails.email = firebaseAuth.getCurrentUser().getEmail();
String photoUrl = firebaseAuth.getCurrentUser().getPhotoUrl().toString();
for (UserInfo profile : firebaseAuth.getCurrentUser().getProviderData()) {
System.out.println(profile.getProviderId());
// check if the provider id matches "facebook.com"
if (profile.getProviderId().equals("facebook.com")) {
String facebookUserId = profile.getUid();
myUserDetails.sigin_provider = profile.getProviderId();
// construct the URL to the profile picture, with a custom height
// alternatively, use '?type=small|medium|large' instead of ?height=
photoUrl = "https://graph.facebook.com/" + facebookUserId + "/picture?height=500";
} else if (profile.getProviderId().equals("google.com")) {
myUserDetails.sigin_provider = profile.getProviderId();
((HomeActivity) getActivity()).loadGoogleUserDetails();
}
}
myUserDetails.profile_picture = photoUrl;
private static final int RC_SIGN_IN = 8888;
public void loadGoogleUserDetails() {
try {
// Configure sign-in to request the user's ID, email address, and basic profile. ID and
// basic profile are included in DEFAULT_SIGN_IN.
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestEmail()
.build();
// Build a GoogleApiClient with access to GoogleSignIn.API and the options above.
mGoogleApiClient = new GoogleApiClient.Builder(this)
.enableAutoManage(this, new GoogleApiClient.OnConnectionFailedListener() {
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
System.out.println("onConnectionFailed");
}
})
.addApi(Auth.GOOGLE_SIGN_IN_API, gso)
.build();
Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
startActivityForResult(signInIntent, RC_SIGN_IN);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// Result returned from launching the Intent from
// GoogleSignInApi.getSignInIntent(...);
if (requestCode == RC_SIGN_IN) {
GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
if (result.isSuccess()) {
GoogleSignInAccount acct = result.getSignInAccount();
// Get account information
String PhotoUrl = acct.getPhotoUrl().toString();
}
}
}
Inside onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) 在onAuthStateChanged内部(@NonNull FirebaseAuth firebaseAuth)
Try If you login with Facebook: 尝试如果您使用Facebook登录:
if (!user.getProviderData().isEmpty() && user.getProviderData().size() > 1)
String URL = "https://graph.facebook.com/" + user.getProviderData().get(1).getUid() + "/picture?type=large";
You can have better resolution profile pics, directly editing the URL for both providers (Google and Facebook): 您可以拥有更好的分辨率配置文件图片,直接编辑两个提供商(Google和Facebook)的网址:
Here is a javascript sample code but, you should be able to easily translate to Java: 这是一个javascript示例代码,但是,您应该能够轻松地转换为Java:
getHigherResProviderPhotoUrl = ({ photoURL, providerId }: any): ?string => {
//workaround to get higer res profile picture
let result = photoURL;
if (providerId.includes('google')) {
result = photoURL.replace('s96-c', 's400-c');
} else if (providerId.includes('facebook')) {
result = `${photoURL}?type=large`;
}
return result;
};
Basically, depending on the provider, you just need to: 基本上,根据提供商,您只需要:
s96-c
in the profile pic url with s400-c
谷歌用s96-c
替换配置文件图片网址中的s400-c
?type=large
at the end of the url 对于Facebook只是追加?type=large
在网址的末尾 For example for google: 例如谷歌:
becomes 变
And for facebook: 对于Facebook:
becomes 变
你有没有尝试过:
Uri xx = FirebaseAuth.getInstance().getCurrentUser().getPhotoUrl();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.