简体   繁体   English

Android:如何修复从 Firebase 拍摄的模糊的 Google 个人资料图片?

[英]Android: How To Fix Blurry Google Profile Image Taken From Firebase?

The scenario:情景:

I am building an app that allows the user to sign in using their Google account, it's worth noting that this is done using Firebase authentication.我正在构建一个允许用户使用他们的 Google 帐户登录的应用程序,值得注意的是,这是使用 Firebase 身份验证完成的。

The problem:问题:

The user signs in perfectly but whenever i get their profile picture it is returned fine but it's very blurry.用户完美登录,但每当我获得他们的个人资料图片时,它都会返回正常但非常模糊。

I've researched and found some sites mentioning to change the resolution of the Google profile image but I'm not sure if that's possible.我研究过并发现一些网站提到要更改 Google 个人资料图片的分辨率,但我不确定这是否可行。 I know Facebook has a graph that allows for you to manipulate the URL of the profile image to change the resolution but I'm not sure how to improve the resolution of the profile image via Google.我知道 Facebook 有一个图表,允许您操纵个人资料图片的 URL 来更改分辨率,但我不确定如何通过 Google 提高个人资料图片的分辨率。

I've posted a screenshot of the blurry image below:我在下面发布了模糊图像的屏幕截图:

在此处输入图像描述

This is the XML code for the ImageView that I'm loading the profile image in:这是我正在加载个人资料图片的 ImageView 的 XML 代码:

<de.hdodenhof.circleimageview.CircleImageView
                xmlns:app="http://schemas.android.com/apk/res-auto"
                android:id="@+id/image_view_profile_pic"
                android:layout_width="96dp"
                android:layout_height="96dp"
                android:layout_gravity="center_horizontal"
                android:layout_marginTop="36dp"
                android:src="@drawable/app_bar_tool_bar_bg"
                app:civ_border_width="2dp"
                app:civ_border_color="#FFFFFFFF"
                  android:contentDescription="@string/content_description_profile_picture" />

This is the code I used to get the profile image from Google via Firebase:这是我用来通过 Firebase 从 Google 获取个人资料图片的代码:

private void getUserProfileContent() {

        // Check if the user is signed in
        if (mUser != null) {
            for (UserInfo profile : mUser.getProviderData()) {

                // Profile photo url
                Uri photoUrl = profile.getPhotoUrl();

                // Load user's profile photo from their signed-in provider into the image view
                Glide.with(ProfileActivity.this)
                        .load(photoUrl)
                        .into(mProfilePic);
            }
        }
    }

This is the code to get the currently authenticated user:这是获取当前经过身份验证的用户的代码:

// Variable declaration
private FirebaseUser mUser;

// Initializatioin in onCreate method
mUser = FirebaseAuth.getInstance().getCurrentUser();

Then of course, I call the above method in the onCreate method so it gets executed, like so:然后当然,我在 onCreate 方法中调用上面的方法以便它被执行,如下所示:

getUserProfileContent();

I have read on this site that Google URLs can be manipulated to change the size of an image:我在这个网站上读到可以操纵谷歌 URL 来改变图像的大小:

Details about Google's "magic" URL 详细了解谷歌的“魔法” URL

Also, I've seen this site mentioning something similar about profile picture being blurry:另外,我看到这个网站提到了类似的关于个人资料图片模糊的内容:

Firebase Google Group: Profile picture blurry Firebase Google 群组:个人资料图片模糊

As it relates to the second link I haven't quite wrapped my head around how to carry out what they've suggested.因为它涉及到第二个链接,所以我还没有完全理解如何执行他们的建议。

I solved my own problem, I will explain in full detail, see below. 我解决了自己的问题,我将详细解释,请参见下文。

Solution: 解:

This is the newly modified code to my method in the question above, this is the fix: 这是上面问题中我的方法的新修改的代码,这是解决方法:

private void getUserProfileContent() {

        // Check if the user is signed in
        if (mUser != null) {
            for (UserInfo profile : mUser.getProviderData()) {

                // Get the profile photo's url
                Uri photoUrl = profile.getPhotoUrl();

                // Variable holding the original String portion of the url that will be replaced
                String originalPieceOfUrl = "s96-c/photo.jpg";

                // Variable holding the new String portion of the url that does the replacing, to improve image quality
                String newPieceOfUrlToAdd = "s400-c/photo.jpg";

                // Check if the Url path is null
                if (photoUrl != null) {

                    // Convert the Url to a String and store into a variable
                    String photoPath = photoUrl.toString();

                    // Replace the original part of the Url with the new part
                    String newString = photoPath.replace(originalPieceOfUrl, newPieceOfUrlToAdd);

                    // Load user's profile photo from their signed-in provider into the image view (with newly edited Url for resolution improvement)
                    Glide.with(ProfileActivity.this)
                            .load(newString)
                            .into(mProfilePic);

                } // End if
            } // End if
        }

Explanation: 说明:

The code is self explanatory but for the sole purpose of clarity for many others who were having issues with this, I basically swapped out the part of the original Url that I retrieved from Google that specified the image's resolution with a higher resolution of my own. 该代码是不言自明的,但是出于清晰起见,对于其他许多对此有疑问的唯一目的,我基本上交换了我从Google检索到的原始网址的一部分,该部分指定了我自己具有更高分辨率的图像分辨率。

Replaced this "s96-c/photo.jpg" with "s400-c/photo.jpg" 将此"s96-c/photo.jpg"替换为"s96-c/photo.jpg" "s400-c/photo.jpg"

I have provided a screenshot below with TextViews displaying: 我在下面提供了一个屏幕截图,其中显示了TextViews:

  1. Url path of the original Url retrieved from Google (this one has the s96-c in its url) 从Google检索到的原始网址的网址路径(此网址的网址中包含s96-c

  2. Url path of the newly modified Url by me (this one has the s400-c in its url) 我新修改的网址的网址路径(此网址中包含s400-c

  3. Original Url retrieved from Google with toString() method called on it. 使用toString()方法从Google检索的原始网址。 This is the third Url in the screenshot with s96-c 这是s96-c屏幕快照中的第三个网址

  4. Newly modified Url by me with toString() method called on it. 我用调用toString()方法新修改了Url。 This is the fourth Url (similarly to the 3rd Url in the screenshot this Url has s96-c in its Url String) 这是第四个网址(类似于该网址在其网址字符串中包含s96-c的屏幕快照中的第三个网址)

In that order 以该顺序

Note: 注意:

I wanted to do an in depth explanation with this answer because I wanted to show that even though the string with low res was replaced with high res version, you'll notice the last TextView displays in its Url s96-c and the second-to-last Url (granted you're looking from top to bottom) displays the exact same thing, s96-c 我想对这个答案做一个深入的解释,因为我想表明即使将低分辨率的字符串替换为高分辨率的版本,您也会注意到Url s96-c中最后一个TextView显示,第二个-last Url(允许您从上到下查看)显示的内容完全相同,即s96-c

BUT

The resolution indeed improved, so my theory is that even though the resolution improved, by me using a higher value, Google's system returned the improved image with a size of s96-c regardless (Let me know if anyone has a more concrete theory) 分辨率确实提高了,所以我的理论是,即使分辨率得到了提高,但我使用更高的值,Google的系统仍会s96-c的大小返回改进后的图像(让我知道是否有人有更具体的理论)

Screenshot of profile image with the blurriness fixed (improved resolution): 修正了模糊度(提高分辨率)的个人资料图像的屏幕截图:

改进了个人资料照片和Urls的分辨率及其在幕后的差异的屏幕快照。

Anyway there you have it, I wanted to show a screenshot with the Urls and their differences so that I can explain how everything was happening, for anyone who was curious. 无论如何,我都想显示一张有关Urls及其差异的屏幕截图,以便我对好奇的人解释一切是如何发生的。

I hope this helps many others, because the blurry Google user profile image was a very inconvenient problem for myself. 我希望这对其他人有帮助,因为模糊的Google用户个人资料图片对我来说是一个非常不便的问题。

In my case, inside of the HTML of my Ionic4 component I used: 就我而言,我在Ionic4组件的HTML中使用了:

<img class="avatar" [src]="user.photoURL + '?height=150'">

Adding the height parameter to the end of the URL worked for me. 将height参数添加到URL的末尾对我有用。

Easy Fix 简易修复

Just replace s96-c with s400-c or any other like s300-c 只需用s400-c或任何其他类似s300-c替换s96-c s300-c

from the image url you get from google. 从您从Google获得的image url获取。

Example: 例:

Here you can easily see the difference.. 在这里,您可以轻松看到区别。

Look at the end of url. 查看url的末尾。

https://lh4.googleusercontent.com/-C0Mqxb50Hxg/AAAAAAAAAAI/AAAAAAAADcw/xuG-pk0PzaI/s96-c/photo.jpg https://lh4.googleusercontent.com/-C0Mqxb50Hxg/AAAAAAAAAAI/AAAAAAAADcw/xuG-pk0PzaI/s96-c/photo.jpg

https://lh4.googleusercontent.com/-C0Mqxb50Hxg/AAAAAAAAAAI/AAAAAAAADcw/xuG-pk0PzaI/s400-c/photo.jpg https://lh4.googleusercontent.com/-C0Mqxb50Hxg/AAAAAAAAAAI/AAAAAAAADcw/xuG-pk0PzaI/s400-c/photo.jpg

Instead of replacing the size, you can also just remove =s96-c from the photoUrl, and you will be receive the max size.除了替换大小,您还可以从 photoUrl 中删除=s96-c ,您将收到最大大小。

I feel the code is much clearer this way 我觉得这种方式的代码更加清晰

 private void onSignedInInitialize(FirebaseUser user) {
    if(user.getDisplayName() != null && user.getDisplayName().length() > 0)
    {
       usernameText.setText(user.getDisplayName());
    }

    String photoUrl;
    String provider = user.getProviders().get(0);
    if (provider.equals("facebook.com")) {
        photoUrl = user.getPhotoUrl() + "?height=500";
    }
    else if(provider.equals("google.com"))
    {
        photoUrl = user.getPhotoUrl().toString();

        //Remove thumbnail url and replace the original part of the Url with the new part
        photoUrl = photoUrl.substring(0, photoUrl.length() - 15) + "s400-c/photo.jpg";

    }
    else
    {
        photoUrl = user.getPhotoUrl().toString();
    }

    Picasso.with(this)
            .load(photoUrl)
            .placeholder(R.drawable.profile_avatar)
            .error(R.drawable.profile_avatar) 
            .into(circleImageView);

}

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

相关问题 如何从 firebase 存储中获取用户个人资料图片 - How to fetch user profile image from firebase storage 如何将 React-Native-camera 拍摄的图像上传到 Firebase 存储? - How do I upload an image taken by React-Native-camera to Firebase storage? 如何从 android 中 firebase 的图像列表中获取单个图像 - how to get single image from list of images from firebase in android 如何使用 firebase 和 Picasso 库更新用户个人资料图片 - how to update user profile image using firebase and Picasso library 在 android 中进行身份验证时,将用户个人资料图片上传到 firebase 时出错 - Having error while uploading user profile image to firebase, during authentication in android 如何使用 firebase 在 android 中的通知中发送图像? - how to send image in notification in android using firebase? 带有从数据库中获取的数据的复选框 - Swift - SwiftUi - Firebase - Check box with the data taken from the database - Swift - SwiftUi - Firebase 如何在不重建的情况下渲染 Firebase 中的图像? - How to render image from Firebase without rebuilding? Firebase Android - 如何以不同用户身份登录(使用 Google 登录后) - Firebase Android - how to login as different user (after signing in with Google) 如何从 Android 应用程序中删除 Firebase 用户? - How to delete a Firebase user from Android App?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM