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

时间:2018-01-03 21:14:38

标签: android firebase firebase-authentication uri google-authentication

情景:

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

问题:

用户完美登录,但每当我获得他们的个人资料图片时,它都会被罚款,但它非常模糊。

我已经研究并发现一些网站提到要更改Google个人资料图片的分辨率,但我不确定这是否可行。我知道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

由于它与第二个链接有关,我还没有完全了解如何执行他们建议的内容。

4 个答案:

答案 0 :(得分: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. 我新修改的网址的网址路径(此网址的网址中包含 s400-c

  3. 使用toString()方法从Google检索到原始网址。这是屏幕截图中第三个包含 s96-c

  4. 的网址
  5. 我用toString()方法调用了新修改的Url。这是第四个Url(与屏幕截图中的第3个Url类似,此Url在其Url字符串中 s96-c

  6. 按此顺序

    注意:

    我想用这个答案做一个深入的解释,因为我想表明即使低分辨率的字符串被高分辨率版本取代,你也会注意到其最后的TextView显示在其网址 s96-c 和倒数第二个网址(授权你从上到下查看)显示完全相同的内容, s96-c

    <强> BUT

    分辨率确实提高了,所以我的理论是,即使分辨率提高,我使用更高的价值,谷歌的系统也会返回尺寸为 s96-c 的改进图像不管(让我知道是否有人有更具体的理论)

    固定模糊度(提高分辨率)的个人资料图片的屏幕截图:

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

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

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

答案 1 :(得分:0)

在我的案例中,我使用了Ionic4组件的HTML:

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

在网址末尾添加height参数对我来说很有效。

答案 2 :(得分:0)

轻松修复

只需将s96-c替换为s400-c或任何其他类似s300-c

从您从Google获得的image url中获取。

示例:

在这里您可以轻松看到区别。

查看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

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

}