安卓 Firebase 认证 - 获取用户头像

18

我该如何检索用户的照片并且使其分辨率足够高,以便从移动应用程序中使用?我查看了指南和API文档,建议的方法似乎是使用FirebaseUser#getPhotoUrl() 。但是,这将返回一个分辨率为50x50像素的照片网址,这对于实用是太低了。客户端是否有一种方式可以请求用户的高分辨率照片呢?我已经单独测试了Facebook登录和Google注册的SDK,并且在这两种情况下,照片的分辨率都比Firebase Auth给出的要高。为什么Firebase Auth会改变原始分辨率?我应该如何防止它这样做?谢谢。

4个回答

25
您可以通过直接编辑两个提供商(Google和Facebook)的URL来获得更好的分辨率的头像图片:
以下是一份JavaScript示例代码,但您应该很容易地将其转化为Java代码:

You can have better resolution profile pics, directly editing the URL for both providers (Google and Facebook):

Here is a javascript sample code but, you should be able to easily translate to 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;
  };

基本上,根据提供者,你只需要:

  • 对于 Google,将个人资料图片的 URL 中的 s96-c 替换为 s400-c
  • 对于 Facebook,只需在 URL 后面添加 ?type=large

例如对于 Google:

低分辨率图片 变成 高分辨率图片

而对于 Facebook:

低分辨率图片 变成 高分辨率图片


2
对于Facebook,您可以将“?height=xxx”附加到链接末尾,以获取所需大小的图片。您还可以输入非常高的高度值,以获取上传到Facebook的原始分辨率照片。 - Jack

12

Facebook和Google的照片网址:

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

            }
        }
    }

我尝试了Facebook部分并成功了。我还没有尝试谷歌部分。谢谢。 - Dika
你在“Facebook-Url”前面加上这个:“https://graph.facebook.com/”但是在Google图片的情况下,你要加什么呢? - KennyAli

9

在 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";

5

你尝试过以下方法吗:

Uri xx = FirebaseAuth.getInstance().getCurrentUser().getPhotoUrl();

是的,我有。那会给你一张50x50像素的照片。 - mobilekid

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接