首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >有时,Picasso没有从URL加载图像

有时,Picasso没有从URL加载图像
EN

Stack Overflow用户
提问于 2017-11-08 10:15:58
回答 2查看 756关注 0票数 0

我的XML代码

代码语言:javascript
复制
<?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.Adapter的代码如下:

代码语言:javascript
复制
@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类的代码如下:

代码语言:javascript
复制
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的日志记录,它将记录以下语句:

代码语言:javascript
复制
    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

一切似乎都是有序的,但照片并没有加载到图像视图,而不是在第一次。但是,如果我远离活动,回来,或者如果屏幕关闭,它们又重新打开,图像就在正确的位置。因此,似乎只要活动恢复,照片就会显示出来。但它并不是加载在第一个实例本身。

我尝试将图像视图的"wrap_content“更改为固定宽度,但行为相同。

我不知道到底出了什么问题。我还能做些什么呢?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-12-17 16:45:07

我终于找到了答案。我已经开始使用fetch预加载应用程序背景中的大多数重要图像。这确保了我不会遇到活动已经加载的情况,但是Picasso仍然在加载图像并在几秒钟内显示占位符图像(有时是永恒的)。典型的例子是

代码语言:javascript
复制
Picasso.with(getBaseContext()).load(tempUser.getPhotoUrl()).fetch();

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

票数 0
EN

Stack Overflow用户

发布于 2017-11-08 12:14:01

如您所见,您可以直接使用字符串而不是uri

这是毕加索图书馆的代码。

代码语言:javascript
复制
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(),就像

代码语言:javascript
复制
Picasso.with(contextFRVA).load(uriBuddy).memoryPolicy(MemoryPolicy.NO_CACHE).into(fellowBuddy.userPhoto);
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47176967

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档