Android Firebase Auth - 获取用户照片

时间:2016-05-23 06:33:13

标签: android firebase firebase-authentication

如何检索可以在移动应用中使用的具有不错分辨率的用户照片?我查看了指南和api文档,推荐的方法似乎是使用FirebaseUser#getPhotoUrl()。然而,这会给出分辨率为50x50像素的照片的网址,该照片太低而无法使用。有没有办法让客户要求更高的用户照片?我已经分别测试了Facebook登录和Google登录的sdks,在这两种情况下,照片的分辨率都高于Firebase Auth的回复。为什么Firebase Auth会更改原始分辨率,如何强制它不要这样做?感谢。

4 个答案:

答案 0 :(得分: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();

            }
        }
    }

答案 1 :(得分: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";

答案 2 :(得分:6)

您可以拥有更好的分辨率配置文件图片,直接编辑两家提供商(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,将个人资料图片网址中的s96-c替换为s400-c
  • 对于Facebook,只需在网址末尾附加?type=large

例如Google

low-res pic变为 high-res pic

对于Facebook:

low-res pic变为 high-res pic

答案 3 :(得分:3)

你试过了吗?

Uri xx = FirebaseAuth.getInstance().getCurrentUser().getPhotoUrl();
相关问题