问题
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));
}
回答1:
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'
回答2:
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);
回答3:
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);
来源:https://stackoverflow.com/questions/42714132/how-to-make-picasso-display-default-image-when-there-is-an-invalid-path