毕加索有时不从 URL 加载图像
Picasso not loading image from a URL, sometimes
我的XML代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="@+id/ip_userpic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/whiteColor"
android:src="@drawable/buddy_menu_icon" />
<TextView
android:id="@+id/ip_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="@string/ip_name_placeholder" />
<Button
android:id="@+id/ip_follow"
style="@style/Widget.AppCompat.Button.Borderless.Colored"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/colorBackground"
android:contentDescription="@string/ip_follow_button"
android:text="@string/ip_follow_button" />
</LinearLayout>
这 XML 在 recyclerview 中加载单行项目。 recyclerView.Adapter 的代码如下:
@Override
public void onBindViewHolder(final BuddyView fellowBuddy, int position) {
---------------- * other code * -----
Uri uriBuddy = Uri.parse(buddyThis.getPhotoUrl());
Picasso.with(contextFRVA).setLoggingEnabled(true);//Todo: Debug profile photo random behaviour
Picasso.with(contextFRVA).load(uriBuddy).into(fellowBuddy.userPhoto);
---------------- * other code * -----
}
我的BuddyView class的代码如下:
class BuddyView extends RecyclerView.ViewHolder {
CircleImage userPhoto;
TextView userName;
String userID;
Button buttonFollow;
BuddyView (View itemView) {
super(itemView);
this.userPhoto = itemView.findViewById(R.id.ip_userpic);
this.userName = itemView.findViewById(R.id.ip_name);
this.buttonFollow = itemView.findViewById(R.id.ip_follow);
}
}
现在,由于我已经为 Picasso 打开日志记录,它正在记录以下语句:
11-08 17:08:17.220 8701-8701/me.buddy.buddy D/Picasso: Main created [R1] Request{https://lh3.googleusercontent.com/-cNRNYVLKaPE/AAAAAAAAAAI/AAAAAAAAFLM/5KchSe2cA7Y/s96-c/photo.jpg}
11-08 17:08:17.220 8701-8962/me.buddy.buddy D/Picasso: Dispatcher enqueued [R1]+4ms
11-08 17:08:17.230 8701-9237/me.buddy.buddy D/Picasso: Hunter executing [R1]+5ms
11-08 17:08:17.240 8701-8701/me.buddy.buddy D/Picasso: Main created [R2] Request{https://lh3.googleusercontent.com/-cNRNYVLKaPE/AAAAAAAAAAI/AAAAAAAAFLM/5KchSe2cA7Y/s96-c/photo.jpg}
11-08 17:08:17.240 8701-8962/me.buddy.buddy D/Picasso: Hunter joined [R2]+1ms to [R1]+19ms, [R2]+1ms
11-08 17:08:17.240 8701-9237/me.buddy.buddy D/Picasso: Hunter decoded [R1]+20ms
11-08 17:08:17.240 8701-8962/me.buddy.buddy D/Picasso: Dispatcher batched [R1]+21ms, [R2]+4ms for completion
11-08 17:08:17.440 8701-8962/me.buddy.buddy D/Picasso: Dispatcher delivered [R1]+222ms, [R2]+205ms
11-08 17:08:17.460 8701-8701/me.buddy.buddy D/Picasso: Main completed [R1]+239ms from DISK
11-08 17:08:17.460 8701-8701/me.buddy.buddy D/Picasso: Main completed [R2]+223ms from DISK
一切似乎都正常,但照片没有加载到图像视图中,一开始没有。但是如果我离开 activity 并返回,或者如果屏幕熄灭然后它们又重新亮起,则图像位于正确的位置。因此,似乎只要 activity 恢复,就会显示照片。但它本身并没有在第一个实例中加载。
我尝试将 imageview 的“wrap_content”更改为固定宽度但行为相同。
我无法弄清楚到底出了什么问题。我可以尝试哪些替代方案?
正如你在这里看到的 你可以直接使用 String 而不是 uri
毕加索库中的代码。
public RequestCreator load(String path) {
if (path == null) {
return new RequestCreator(this, null, 0);
}
if (path.trim().length() == 0) {
throw new IllegalArgumentException("Path must not be empty.");
}
return load(Uri.parse(path));
}
关于您的问题,您是否尝试过 memoryPolicy(),这类似于
Picasso.with(contextFRVA).load(uriBuddy).memoryPolicy(MemoryPolicy.NO_CACHE).into(fellowBuddy.userPhoto);
我终于找到了这个问题的答案。我已经开始使用 fetch
在应用程序的后台预加载大部分重要图像。这确保我不会 运行 进入 activity 已加载但 Picasso 仍在加载图像并显示占位符图像几秒钟(有时甚至是永恒)的情况。典型的例子是
Picasso.with(getBaseContext()).load(tempUser.getPhotoUrl()).fetch();
鉴于 Picasso 出色的缓存和图像加载能力,使用 fetch 预加载一些臭名昭著的图像可提供出色的用户体验。
我的XML代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="@+id/ip_userpic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/whiteColor"
android:src="@drawable/buddy_menu_icon" />
<TextView
android:id="@+id/ip_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="@string/ip_name_placeholder" />
<Button
android:id="@+id/ip_follow"
style="@style/Widget.AppCompat.Button.Borderless.Colored"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/colorBackground"
android:contentDescription="@string/ip_follow_button"
android:text="@string/ip_follow_button" />
</LinearLayout>
这 XML 在 recyclerview 中加载单行项目。 recyclerView.Adapter 的代码如下:
@Override
public void onBindViewHolder(final BuddyView fellowBuddy, int position) {
---------------- * other code * -----
Uri uriBuddy = Uri.parse(buddyThis.getPhotoUrl());
Picasso.with(contextFRVA).setLoggingEnabled(true);//Todo: Debug profile photo random behaviour
Picasso.with(contextFRVA).load(uriBuddy).into(fellowBuddy.userPhoto);
---------------- * other code * -----
}
我的BuddyView class的代码如下:
class BuddyView extends RecyclerView.ViewHolder {
CircleImage userPhoto;
TextView userName;
String userID;
Button buttonFollow;
BuddyView (View itemView) {
super(itemView);
this.userPhoto = itemView.findViewById(R.id.ip_userpic);
this.userName = itemView.findViewById(R.id.ip_name);
this.buttonFollow = itemView.findViewById(R.id.ip_follow);
}
}
现在,由于我已经为 Picasso 打开日志记录,它正在记录以下语句:
11-08 17:08:17.220 8701-8701/me.buddy.buddy D/Picasso: Main created [R1] Request{https://lh3.googleusercontent.com/-cNRNYVLKaPE/AAAAAAAAAAI/AAAAAAAAFLM/5KchSe2cA7Y/s96-c/photo.jpg}
11-08 17:08:17.220 8701-8962/me.buddy.buddy D/Picasso: Dispatcher enqueued [R1]+4ms
11-08 17:08:17.230 8701-9237/me.buddy.buddy D/Picasso: Hunter executing [R1]+5ms
11-08 17:08:17.240 8701-8701/me.buddy.buddy D/Picasso: Main created [R2] Request{https://lh3.googleusercontent.com/-cNRNYVLKaPE/AAAAAAAAAAI/AAAAAAAAFLM/5KchSe2cA7Y/s96-c/photo.jpg}
11-08 17:08:17.240 8701-8962/me.buddy.buddy D/Picasso: Hunter joined [R2]+1ms to [R1]+19ms, [R2]+1ms
11-08 17:08:17.240 8701-9237/me.buddy.buddy D/Picasso: Hunter decoded [R1]+20ms
11-08 17:08:17.240 8701-8962/me.buddy.buddy D/Picasso: Dispatcher batched [R1]+21ms, [R2]+4ms for completion
11-08 17:08:17.440 8701-8962/me.buddy.buddy D/Picasso: Dispatcher delivered [R1]+222ms, [R2]+205ms
11-08 17:08:17.460 8701-8701/me.buddy.buddy D/Picasso: Main completed [R1]+239ms from DISK
11-08 17:08:17.460 8701-8701/me.buddy.buddy D/Picasso: Main completed [R2]+223ms from DISK
一切似乎都正常,但照片没有加载到图像视图中,一开始没有。但是如果我离开 activity 并返回,或者如果屏幕熄灭然后它们又重新亮起,则图像位于正确的位置。因此,似乎只要 activity 恢复,就会显示照片。但它本身并没有在第一个实例中加载。
我尝试将 imageview 的“wrap_content”更改为固定宽度但行为相同。
我无法弄清楚到底出了什么问题。我可以尝试哪些替代方案?
正如你在这里看到的 你可以直接使用 String 而不是 uri
毕加索库中的代码。
public RequestCreator load(String path) {
if (path == null) {
return new RequestCreator(this, null, 0);
}
if (path.trim().length() == 0) {
throw new IllegalArgumentException("Path must not be empty.");
}
return load(Uri.parse(path));
}
关于您的问题,您是否尝试过 memoryPolicy(),这类似于
Picasso.with(contextFRVA).load(uriBuddy).memoryPolicy(MemoryPolicy.NO_CACHE).into(fellowBuddy.userPhoto);
我终于找到了这个问题的答案。我已经开始使用 fetch
在应用程序的后台预加载大部分重要图像。这确保我不会 运行 进入 activity 已加载但 Picasso 仍在加载图像并显示占位符图像几秒钟(有时甚至是永恒)的情况。典型的例子是
Picasso.with(getBaseContext()).load(tempUser.getPhotoUrl()).fetch();
鉴于 Picasso 出色的缓存和图像加载能力,使用 fetch 预加载一些臭名昭著的图像可提供出色的用户体验。