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

问题描述 投票:1回答:4

场景:

我正在构建一个允许用户使用他们的Google帐户登录的应用,值得注意的是,这是使用Firebase身份验证完成的。

问题:

用户完美地登录,但每当我得到他们的个人资料图片时,它都会很好地返回,但它非常模糊。

我研究并发现一些网站提到改变谷歌个人资料图片的分辨率,但我不确定这是否可行。我知道Facebook有一个图表,允许你操纵配置文件图像的URL来改变分辨率,但我不知道如何通过谷歌提高配置文件图像的分辨率。

我发布了下面模糊图片的截图:

enter image description here

这是我正在加载配置文件图像的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" />

这是我用来通过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);
            }
        }
    }

这是获取当前经过身份验证的用户的代码:

// Variable declaration
private FirebaseUser mUser;

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

然后,当然,我在onCreate方法中调用上面的方法,所以它被执行,如下所示:

getUserProfileContent();

我在此网站上看到,可以操纵Google网址来更改图片的大小:

Details about Google's "magic" URL

此外,我已经看到这个网站提到类似的配置文件图像模糊:

Firebase Google Group: Profile picture blurry

由于它涉及第二个链接,我还没有完全围绕如何执行他们建议的内容。

android firebase firebase-authentication uri google-authentication
4个回答
7
投票

我解决了自己的问题,我将详细解释,见下文。

解:

这是上面问题中我方法新修改的代码,这是修复:

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
        }

说明:

代码是自我解释的,但为了清楚地说明许多其他有问题的人,我基本上换掉了我从Google检索到的原始Url部分,它指定了我自己的更高分辨率的图像分辨率。

"s96-c/photo.jpg"替换了这个"s400-c/photo.jpg"

我在下面的TextViews中提供了一个截图:

  1. 从Google检索到的原始网址的网址路径(此网址在其网址中包含s96-c)
  2. 我修改过的Url的Url路径(这个url中有s400-c)
  3. 使用toString()方法从Google检索到原始网址。这是s96-c屏幕截图中的第三个Url
  4. 我用toString()方法对它进行了新修改的Url。这是第四个Url(类似于屏幕截图中的第3个Url,此Url在其Url字符串中有s96-c)

以该顺序

注意:

我想用这个答案做一个深入的解释,因为我想表明即使低分辨率的字符串被高分辨率版本替换,你会注意到最后的TextView显示在其Url s96-c中,而第二个是-last Url(授予你从上到下看)显示完全相同的东西,s96-c

分辨率确实提高了,所以我的理论是,即使分辨率提高了,我使用更高的值,谷歌的系统返回了改进的图像,大小为s96-c,无论如何(让我知道是否有人有更具体的理论)

固定模糊度的配置文件图像的屏幕截图(提高了分辨率):

Screenshot of improved resolution of profile photo and of the Urls and their differences behind the scenes.

无论如何,你有它,我想显示一个截图与网址和他们的差异,以便我可以解释一切发生的事情,对于任何好奇的人。

我希望这有助于其他许多人,因为模糊的Google用户个人资料图片对我来说是一个非常不方便的问题。


0
投票

在我的例子中,我使用的Ionic4组件的HTML内部:

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

将height参数添加到URL的末尾对我有用。


0
投票

轻松修复

只需用s96-c或任何其他像s400-c替换s300-c

来自你从谷歌获得的image url

例:

在这里您可以轻松看到差异..

看看网址的结尾。

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


-1
投票

我觉得代码更加清晰

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

}
© www.soinside.com 2019 - 2024. All rights reserved.