简体   繁体   English

Android Firebase Auth - 获取用户照片

[英]Android Firebase Auth - Get User's Photo

How can I retrieve a photo of the user with decent resolution that can be used from a mobile app? 如何从移动应用程序中检索具有相当分辨率的用户照片? I looked at the guides and the api docs and the recommended way seemed to be to use FirebaseUser#getPhotoUrl() . 我查看了指南和api文档,推荐的方法似乎是使用FirebaseUser#getPhotoUrl() This however gives back a url to a photo with resolution 50x50 px, which is too low to be useful. 然而,这会给出分辨率为50x50像素的照片的网址,该照片太低而无法使用。 Is there a way for the client to request a higher res photo of the user? 有没有办法让客户要求更高的用户照片? I've tested the sdks of Facebook Login and Google Sign-in separately, and in both cases the resolutions of the photos are higher than what Firebase Auth gives back. 我已经分别测试了Facebook登录和Google登录的sdks,在这两种情况下,照片的分辨率都高于Firebase Auth的回复。 Why does Firebase Auth change the original resolutions and how can I force it not to do that? 为什么Firebase Auth会更改原始分辨率,如何强制它不要这样做? Thanks. 谢谢。

Facebook and Google PhotoURL : 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();

            }
        }
    }

Inside onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) 在onAuthStateChanged内部(@NonNull FirebaseAuth firebaseAuth)

Try If you login with Facebook: 尝试如果您使用Facebook登录:

 if (!user.getProviderData().isEmpty() && user.getProviderData().size() > 1)
                String URL = "https://graph.facebook.com/" + user.getProviderData().get(1).getUid() + "/picture?type=large";

You can have better resolution profile pics, directly editing the URL for both providers (Google and Facebook): 您可以拥有更好的分辨率配置文件图片,直接编辑两个提供商(Google和Facebook)的网址:

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

Basically, depending on the provider, you just need to: 基本上,根据提供商,您只需要:

  • for google replace s96-c in the profile pic url with s400-c 谷歌用s96-c替换配置文件图片网址中的s400-c
  • for facebook just append ?type=large at the end of the url 对于Facebook只是追加?type=large在网址的末尾

For example for google: 例如谷歌:

低分辨率的照片 becomes 高分辨率照片

And for facebook: 对于Facebook:

低分辨率的照片 becomes 高分辨率照片

你有没有尝试过:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Firebase身份验证:Android:无法显示用户电子邮件和照片 - Firebase Auth: Android: Unable to display user email and photo 如何获取谷歌用户照片? Android Firebase - How to get google user photo? Android Firebase Firebase Auth:我想获取当前用户的个人资料照片并将其上传到 firebase 数据库或存储 - Firebase Auth: I want to get the current user profile photo and upload it to firebase database or storage 通过 Firebase 在 Android 应用程序中进行身份验证:获取用户的名字和姓氏 - Auth in the Android app via Firebase: Get the user’s first and last name Firebase Auth获取用户所在国家 - Firebase Auth Get User Country 获取用户电子邮件Firebase身份验证 - Get the user email firebase auth 如何在 Android 中使用 .setPhotoUri 从本地存储添加图片以更新 Firebase 用户的照片配置文件? - How to add a picture from local storage to update Firebase user's photo profile using .setPhotoUri in Android? Firebase:获取Facebook照片网址[Android] - Firebase : Get Facebook Photo Url [ Android ] 在Firebase Auth(android)之后在数据库中创建用户 - Create user in database after Firebase Auth (android) Android - Firebase用户验证令牌是否过期? - Android - Do Firebase User Auth Tokens Expire?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM