【问题标题】:Android Firebase Auth - Get User's PhotoAndroid Firebase Auth - 获取用户照片
【发布时间】:2016-05-23 06:33:13
【问题描述】:

我如何才能从移动应用中检索出具有良好分辨率的用户照片?我查看了指南和 api 文档,推荐的方法似乎是使用 FirebaseUser#getPhotoUrl()。然而,这会返回一个分辨率为 50x50 像素的照片的 URL,这太低而无法使用。客户有没有办法请求用户的更高分辨率照片?我分别测试了 Facebook Login 和 Google Sign-in 的 sdks,在这两种情况下,照片的分辨率都高于 Firebase Auth 返回的分辨率。为什么 Firebase Auth 会更改原始分辨率,我该如何强制它不这样做?谢谢。

【问题讨论】:

    标签: android firebase firebase-authentication


    【解决方案1】:

    您可以获得分辨率更高的个人资料图片,直接编辑两个提供商(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;
      };
    

    基本上,根据提供商的不同,您只需:

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

    例如对于谷歌:

    变为

    对于 facebook:

    变为

    【讨论】:

    • 对于 Facebook,您可以改为附加 "?height=xxx" 以获取所需大小的图片。您也可以输入一个非常高的高度值来获取上传到Facebook的原始res照片。
    【解决方案2】:

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

    【讨论】:

    • 我在 facebook 部分试过这个并成功了。我还没有尝试谷歌部分。谢谢
    • 你在 de 'Facebook-Url' 之前放了这个:“graph.facebook.com”但是你在谷歌图片的情况下放了什么?
    【解决方案3】:

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

    【讨论】:

      【解决方案4】:

      你试过了吗:

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

      【讨论】:

      • 是的,我有。这会给你一张 50x50 像素的照片。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-19
      • 2014-12-23
      • 1970-01-01
      • 2018-12-16
      • 1970-01-01
      相关资源
      最近更新 更多