Android Firebase Auth - 获取用户照片

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)


Gom*_*ino 9

您可以拥有更好的分辨率配置文件图片,直接编辑两个提供商(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)

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

  • 对于google s96-c,将个人资料图片网址替换为s400-c
  • 对于facebook,只需?type=large在网址末尾附加

以google为例:

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

对于facebook:

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

  • 然后谷歌的业务和面对朋友:) (2认同)
  • 对于 Facebook,您可以改为附加“?height=xxx”以获得您想要的尺寸的图片。您也可以输入一个非常高的高度值来获取上传到 Facebook 的原始 res 照片。 (2认同)

Jua*_*dor 8

在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)


Ers*_*III 5

你有没有尝试过:

Uri xx = FirebaseAuth.getInstance().getCurrentUser().getPhotoUrl();
Run Code Online (Sandbox Code Playgroud)