how to make picasso display default image when there is an invalid path

落花浮王杯 提交于 2019-12-01 22:16:33

Try this,

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

gradle:

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

Try this,

    Picasso.with(this)
     .load("YOUR IMAGE URL HERE")        
     .placeholder(DRAWABLE RESOURCE)       
     .error(DRAWABLE RESOURCE)      // Image to load when something goes wrong 
     .resize(width, height)                            
     .rotate(degree)                                 
     .into(imageView);

Picasso are not manging android memorey & slow , so i am higley recommend you to not to use it and instade of it just use Glide libeary .

(You can download image and make her circle with glide , download image a Bitmap and much more )

add to Gradle:

repositories { mavenCentral() // jcenter() works as well because it pulls from Maven Central }

dependencies { compile 'com.github.bumptech.glide:glide:3.7.0'
compile 'com.android.support:support-v4:19.1.0' }

and just:

for load a pic:

Glide.with(this).load("URL").into(imageView);

and you can add a lot more functionality by glide , for example if you want a placeholder until the photo will load you are just adding .placeholder(): :

Glide.with(this).load("URL").placeholder(R.drawable.placeholder).into(imageView);

and if you want a to display photo incase of the photo you are trying to load is broken or have any problem , just add .error()

:

Glide.with(this).load("URL").error(R.drawable.error).placeholder(R.drawable.placeholder).into(imageView);

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!