android - 当存在无效路径时如何使 picasso 显示默认图像

标签 android picasso

我在此处显示默认图像名称时遇到一个问题 R.drawable.avatar_placeholder。当来自 webservice 的链接不为空时,但错误 404. 表示该链接路径上没有任何图像。如果我在下面运行此函数,则会显示字符串“path not empty”,但无法显示图像。欢迎任何建议。谢谢。

private void loadProfileDetails() {
        Logger.d(UI_LoginFragmentWithPin.class, "loadProfileDetails profile image: " + PrefUtils.readString(Constant.PREF_PROFILE_IMAGE));
        if (!TextUtils.isEmpty(PrefUtils.readString(Constant.PREF_PROFILE_IMAGE))){
            Utils.println("path not empty");
            LPicasso.getInstance(getActivity())
                    .load(PrefUtils.readString(Constant.PREF_PROFILE_IMAGE))
                    .config(Bitmap.Config.RGB_565)
                    .resize(200, 200)
                    .centerCrop()
                    .into(imgProfile);
        }else {
            Utils.println("path empty");
            LPicasso.getInstance(getActivity())
                    .load(R.drawable.avatar_placeholder)
                    .config(Bitmap.Config.RGB_565)
                    .resize(200, 200)
                    .centerCrop()
                    .into(imgProfile);

        }
        tvEmail.setText(PrefUtils.readString(Constant.PREF_EMAIL));
        tvName.setText(PrefUtils.readString(Constant.PREF_USER_NAME) + " " + PrefUtils.readString(Constant.PREF_USER_SURNAME));
    }

最佳答案

试试这个,

将错误图像设置为占位符图像

    if(!PrefUtils.readString(Constant.PREF_PROFILE_IMAGE).equals("")) 
    {
        Picasso.with(context).load(PrefUtils.readString(Constant.PREF_PROFILE_IMAGE)).resize(200,200).centerCrop().error(R.drawable.avatar_placeholder).into(imgProfile);
    }
    else
    {
        Picasso.with(context).load(R.drawable.avatar_placeholder).error(R.drawable.avatar_placeholder).resize(200,200).centerCrop().into(imgProfile);
    }

渐变:

    compile 'com.squareup.picasso:picasso:2.5.2'

关于android - 当存在无效路径时如何使 picasso 显示默认图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42714132/

相关文章:

android - 在 Scrollview 中使用 FragmentStatePagerAdapter、Viewpager 的问题

android - 手动添加 Firefox for android 插件

android - 使用 Picasso 从 Firebase Storage 加载图像到 Infowindow 中的 ImageView,Picasso 只显示占位符

Android ImageView ,图像在左上角被截断,如何避免 id?

Android,使用ffmpeg将gif分割成帧

android - 移除@Override 注解错误

java - 如何使用 Picasso 加载 Google map 静态 map ?

java - picasso 图书馆加载我的图片太慢了?

android - 从使用 Picasso 加载的 ImageView 获取位图

android - 如何授予对关联/辅助 Google 帐户的访问权限