Android:如何修复从 Firebase 获取的模糊 Google 个人资料图像?
Android: How To Fix Blurry Google Profile Image Taken From Firebase?
场景:
我正在构建一个允许用户使用他们的 Google 帐户登录的应用程序,值得注意的是,这是使用 Firebase 身份验证完成的。
问题:
用户登录完美,但每当我得到他们的个人资料照片时,它返回正常,但非常模糊。
我研究过并发现一些网站提到要更改 Google 个人资料图片的分辨率,但我不确定这是否可行。我知道 Facebook 有一个图表,允许您操纵个人资料图片的 URL 来更改分辨率,但我不确定如何通过 Google.[= 提高个人资料图片的分辨率。 19=]
我在下面发布了模糊图像的屏幕截图:
这是我正在加载个人资料图片的 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 URLs 来改变图像的大小:
Details about Google's "magic" URL
另外,我看到这个网站提到了类似的关于个人资料图片模糊的事情:
Firebase Google Group: Profile picture blurry
因为它与第二个 link 有关,所以我还没有完全弄清楚如何执行他们的建议。
我解决了我自己的问题,我会详细解释,见下文。
解法:
这是我在上面问题中的方法新修改的代码,这是修复:
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"
我在下方提供了一张显示 TextView 的屏幕截图:
Url 从 Google 中检索到的原始 Url 路径(这个在 s96-c它的 url)
Url 我新修改的Url路径(这个s400-c在url)
原始 Url 从 Google 中检索到并调用了 toString()
方法。这是截图中第三个Url带s96-c
我新修改了 Url 并调用了 toString()
方法。这是第四个 Url(类似于屏幕截图中的第 3 个 Url 此 Url 在其 Url 字符串中具有 s96-c )
按顺序
注:
我想对这个答案做一个深入的解释,因为我想表明即使低分辨率的字符串被高分辨率版本替换,你会注意到最后一个 TextView 显示在它的 Url s96-c 和倒数第二个 Url(假设您从上到下查看)显示完全相同的内容,s96- c
但是
分辨率确实提高了,所以我的理论是,即使分辨率提高了,但我使用更高的值,Google的系统返回了尺寸为 s96 的改进图像-c 无论如何(让我知道是否有人有更具体的理论)
已修复模糊度(提高分辨率)的个人资料图片屏幕截图:
无论如何你已经知道了,我想展示一个包含 Url 及其差异的屏幕截图,以便我可以向所有好奇的人解释一切是如何发生的。
我希望这对其他人有帮助,因为模糊的 Google 用户个人资料图片对我自己来说是一个非常不便的问题。
感觉这样代码更清晰
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);
}
就我而言,在我使用的 Ionic4 组件的 HTML 内部:
<img class="avatar" [src]="user.photoURL + '?height=150'">
将高度参数添加到 URL 的末尾对我有用。
轻松修复
只需将 s96-c
替换为 s400-c
或任何其他类似 s300-c
从 image url
你从 google 得到。
示例:
在这里你可以很容易地看出区别..
看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
除了替换尺寸,您还可以只从 photoUrl 中删除 =s96-c
,您将收到最大尺寸。
场景:
我正在构建一个允许用户使用他们的 Google 帐户登录的应用程序,值得注意的是,这是使用 Firebase 身份验证完成的。
问题:
用户登录完美,但每当我得到他们的个人资料照片时,它返回正常,但非常模糊。
我研究过并发现一些网站提到要更改 Google 个人资料图片的分辨率,但我不确定这是否可行。我知道 Facebook 有一个图表,允许您操纵个人资料图片的 URL 来更改分辨率,但我不确定如何通过 Google.[= 提高个人资料图片的分辨率。 19=]
我在下面发布了模糊图像的屏幕截图:
这是我正在加载个人资料图片的 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 URLs 来改变图像的大小:
Details about Google's "magic" URL
另外,我看到这个网站提到了类似的关于个人资料图片模糊的事情:
Firebase Google Group: Profile picture blurry
因为它与第二个 link 有关,所以我还没有完全弄清楚如何执行他们的建议。
我解决了我自己的问题,我会详细解释,见下文。
解法:
这是我在上面问题中的方法新修改的代码,这是修复:
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"
我在下方提供了一张显示 TextView 的屏幕截图:
Url 从 Google 中检索到的原始 Url 路径(这个在 s96-c它的 url)
Url 我新修改的Url路径(这个s400-c在url)
原始 Url 从 Google 中检索到并调用了
toString()
方法。这是截图中第三个Url带s96-c我新修改了 Url 并调用了
toString()
方法。这是第四个 Url(类似于屏幕截图中的第 3 个 Url 此 Url 在其 Url 字符串中具有 s96-c )
按顺序
注:
我想对这个答案做一个深入的解释,因为我想表明即使低分辨率的字符串被高分辨率版本替换,你会注意到最后一个 TextView 显示在它的 Url s96-c 和倒数第二个 Url(假设您从上到下查看)显示完全相同的内容,s96- c
但是
分辨率确实提高了,所以我的理论是,即使分辨率提高了,但我使用更高的值,Google的系统返回了尺寸为 s96 的改进图像-c 无论如何(让我知道是否有人有更具体的理论)
已修复模糊度(提高分辨率)的个人资料图片屏幕截图:
无论如何你已经知道了,我想展示一个包含 Url 及其差异的屏幕截图,以便我可以向所有好奇的人解释一切是如何发生的。
我希望这对其他人有帮助,因为模糊的 Google 用户个人资料图片对我自己来说是一个非常不便的问题。
感觉这样代码更清晰
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);
}
就我而言,在我使用的 Ionic4 组件的 HTML 内部:
<img class="avatar" [src]="user.photoURL + '?height=150'">
将高度参数添加到 URL 的末尾对我有用。
轻松修复
只需将 s96-c
替换为 s400-c
或任何其他类似 s300-c
从 image url
你从 google 得到。
示例:
在这里你可以很容易地看出区别..
看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
除了替换尺寸,您还可以只从 photoUrl 中删除 =s96-c
,您将收到最大尺寸。