【问题标题】:picasso doesn't display picture in my ImageView what should I do?毕加索没有在我的 ImageView 中显示图片我该怎么办?
【发布时间】:2022-01-06 07:02:32
【问题描述】:

我可以加载图像 URL,但 picasso 或 glide 无法在 imageView 中显示它们(尝试两者)。任何解决方案? 我认为我的类或适配器有问题,因为当我调用它们时,它们中的一些返回 null,我不知道它有什么问题,但我可以得到 ImageUrl,我检查了拼写,没关系。

String imageUri = photo.getPhotoURL().getRegular();
    Log.d("photo_log"," : "+imageUri);
    Picasso.get().load(imageUri).into(holder.photo);

这是我的日志

 D/photo_log:  : https://images.unsplash.com/photo-1633113089631-6456cccaadad?crop=entropy&cs=tinysrgb&fit=max&fm=jpg&ixid=MnwyNzc5MDJ8MXwxfGFsbHwxfHx8fHx8Mnx8MTYzODEyNDQ3MQ&ixlib=rb-1.2.1&q=80&w=1080

这是我的照片课

package com.example.wallpaperapp.Model;

import com.google.gson.annotations.SerializedName;

public class Photo {

@SerializedName("id")
private String id;
@SerializedName("description")
private String description;
@SerializedName("urls")
private PhotoURL photoURL=new PhotoURL();
@SerializedName("users")
private User user=new User();

public String getId() {
    return id;
}

public void setId(String id) {
    this.id = id;
}

public String getDescription() {
    return description;
}

public void setDescription(String description) {
    this.description = description;
}

public PhotoURL getPhotoURL() {
    return photoURL;
}

public void setPhotoURL(PhotoURL photoURL) {
    this.photoURL = photoURL;
}

public User getUser() {
    return user;
}

public void setUser(User user) {
    this.user = user;
}
}

这是我的用户类

package com.example.wallpaperapp.Model;

import com.google.gson.annotations.SerializedName;

public class User {

@SerializedName("first_name")
private String firstName;
@SerializedName("last_name")
private String lastName;
@SerializedName("id")
private String id;
@SerializedName("profile_image")
private ProfileImage profileImage=new ProfileImage();

public String getFirstName() {
    return firstName;
}

public void setFirstName(String firstName) {
    this.firstName = firstName;
}

public String getLastName() {
    return lastName;
}

public void setLastName(String lastName) {
    this.lastName = lastName;
}

public String getId() {
    return id;
}

public void setId(String id) {
    this.id = id;
}

public ProfileImage getProfileImage() {
    return profileImage;
}

public void setProfileImage(ProfileImage profileImage) {
    this.profileImage = profileImage;
}
}

这是我的适配器

public class photoAdapter extends RecyclerView.Adapter<photoAdapter.ViewHolder> {


private Context context;
private List<Photo> photos;


public photoAdapter(Context context, List<Photo> photos) {
    this.context = context;
    this.photos = photos;
}




 @NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.photo_item, parent, false);
        return new ViewHolder(view);
    }

@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {

    Photo photo = photos.get(position);
    holder.userName.setText(photo.getId());
    String imageUri = photo.getPhotoURL().getRegular();
    Picasso.get().load(imageUri).into(holder.photo);
}

@Override
public int getItemCount() {
    return photos.size();
}

public class ViewHolder extends RecyclerView.ViewHolder {

    CircleImageView userAvatar;
    TextView userName;
    ImageView photo;

    public ViewHolder(@NonNull View itemView) {
        super(itemView);
        userAvatar = itemView.findViewById(R.id.item_user_avatar);
        userName = itemView.findViewById(R.id.item_user_name);
        photo = itemView.findViewById(R.id.photo_item_recycler);



       }
    }
}

我也把网址直接放到.load(),但还是不显示图片

【问题讨论】:

  • 你能把代码贴在你设置图片网址的地方吗?
  • 是的,我添加了,请再检查一遍
  • 当图片 url 使用 https 时,Picasso 将无济于事,尝试使用 glide 代替
  • 我用了 glide 也没有改变结果
  • 您是否在 Manifest 中添加了 Internet 权限? &lt;uses-permission android:name="android.permission.INTERNET"/&gt;

标签: java android picasso android-glide


【解决方案1】:

你可以试试这个以获得有关错误的更多信息

Picasso.with(context)
      .load(imageUri)
      .into(holder.photo, new Callback() {
                @Override
                public void onSuccess() {
                  // add log
                }

                @Override
                public void onError(Exception e) {
                  // add log and printstack trace
                }
            })

您还可以从可绘制对象中设置“错误图像”,以查看问题是否来自您的适配器:

Picasso.with(context).load(imageUri)
    .error(R.drawable.my_error_image)
    .into(imageView);

【讨论】:

  • 谢谢,我发现了错误
猜你喜欢
  • 2015-11-14
  • 2019-04-06
  • 2020-05-02
  • 1970-01-01
  • 2021-08-09
  • 2022-09-24
  • 1970-01-01
  • 2021-10-10
  • 1970-01-01
相关资源
最近更新 更多