路径无效时如何使毕加索显示默认图像 [英] how to make picasso display default image when there is an invalid path

查看:70
本文介绍了路径无效时如何使毕加索显示默认图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里显示一个默认图像名称R.drawable.avatar_placeholder时遇到问题.如果来自Web服务的链接非空,但错误404.表示该链接路径上没有任何图像.如果我在下面运行此功能,则会显示字符串"path not empty",但无法显示图像.任何建议都欢迎.谢谢.

I am having one issue on display a default image name here R.drawable.avatar_placeholder. When the link from webservice is non empty, but error 404. means there isn't any image on that link path. Which if i run this function below, the string "path not empty" is shown, but it failed to display the image. Any suggestion is welcomed. Thanks.

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

推荐答案

尝试一下,

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

Set error image as your place holder image

    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'

这篇关于路径无效时如何使毕加索显示默认图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆