mob*_*kid 9 android firebase firebase-authentication
如何从移动应用程序中检索具有相当分辨率的用户照片?我查看了指南和api文档,推荐的方法似乎是使用FirebaseUser#getPhotoUrl().然而,这会给出分辨率为50x50像素的照片的网址,该照片太低而无法使用.有没有办法让客户要求更高的用户照片?我已经分别测试了Facebook登录和Google登录的sdks,在这两种情况下,照片的分辨率都高于Firebase Auth的回复.为什么Firebase Auth会更改原始分辨率,如何强制它不要这样做?谢谢.
Jit*_*yan 10
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();
}
}
}
Run Code Online (Sandbox Code Playgroud)
您可以拥有更好的分辨率配置文件图片,直接编辑两个提供商(Google和Facebook)的URL:
这是一个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;
};
Run Code Online (Sandbox Code Playgroud)
基本上,根据提供者,您只需要:
s96-c,将个人资料图片网址替换为s400-c?type=large在网址末尾附加以google为例:
变成

对于facebook:
变成
在onAuthStateChanged内部(@NonNull FirebaseAuth firebaseAuth)
尝试如果您使用Facebook登录:
if (!user.getProviderData().isEmpty() && user.getProviderData().size() > 1)
String URL = "https://graph.facebook.com/" + user.getProviderData().get(1).getUid() + "/picture?type=large";
Run Code Online (Sandbox Code Playgroud)
你有没有尝试过:
Uri xx = FirebaseAuth.getInstance().getCurrentUser().getPhotoUrl();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
12846 次 |
| 最近记录: |