毕加索有时候不会从URL加载图片

时间:2017-11-08 10:15:58

标签: android picasso

我的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 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);
    }
}

现在,因为我已经开启了毕加索的日志记录,它正在记录下面的语句

    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

所有似乎都是有序的,但照片没有加载到imageview中,而不是第一次加载。但如果我离开活动并返回,或者如果屏幕熄灭并且它们重新开启,则图像处于适当的位置。因此,似乎任何时候活动恢复,照片显示。但它并没有在第一个实例中加载。

我尝试更改&#34; wrap_content&#34; imageview到固定宽度但行为相同。

所以我无法确切地知道出了什么问题。任何人都可以帮忙看看上面的代码并提出一些我可以尝试的替代方案吗?

非常感谢帮助。

2 个答案:

答案 0 :(得分:1)

如您所见,您可以直接使用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);

答案 1 :(得分:0)

我终于找到了答案。我已经开始使用println list[i] INSTEAD OF bat "${flow_bm_folder}\\Env.bat ${list[i]}" 在应用程序的后台预加载大多数重要图像。这样可以确保我没有遇到已加载活动的情况,但Picasso仍在加载图像并显示占位符图像几秒钟(或有时是永恒的)。典型的例子是

fetch

鉴于Picasso出色的缓存和图像加载能力,使用fetch预加载一些臭名昭着的图像可以获得出色的用户体验。