How do I set placeholder if the image is null? - android

I get an image from the server and use Picasso to set it in ImageView. The image can be null so I use let. But when the image is null I get an error. How can I use let to set placeholder if the image is null?
My function:
contactsModel.images?.let { url ->
Picasso.with(itemView.context).load(url)
.placeholder(R.drawable.ic_person_placeholder)
.into(mContactIcon) }
My error:
java.lang.IllegalArgumentException: Path must not be empty.
at com.squareup.picasso.Picasso.load(Picasso.java:297)

Placeholder is used for holding image for the network image. If you want to use the placeholder image when there is a error loading image in the view then kindly use it with error call.
.error(R.drawable.ic_person_placeholder)
as shown in the answer https://futurestud.io/tutorials/picasso-placeholders-errors-and-fading

there are many ways but you can simply check if the URL is empty. then use an image from drawable and add to your image view. use something like this -
if (url.isEmpty()) {
iview.setImageResource(R.drawable.placeholder);
} else{
Picasso.get().load(url).into(iview);
}

Related

I am loading image in Glide but Glide not working if url contains space

I am loading image in glide having spacing in it but facing some issue.
I remove white spaces but image not loaded.
image.replaceAll(" ", "%20"))
The official documentation says, that replaceAll() method doesn't modify your string, but returns a new modified string. So you need to use it like this:
image = image.replaceAll(" ", "%20"))

Why when loading image with Glide as ByteArray into an ImageView doesn't rotate it according to EXIF?

I am using Glide library on Android to load a JPG format image into an ImageView, first I convert it to a ByteArray and then I use the following code:
GlideApp.with(context)
.load(selectedImageByteArray)
.into(image_view)
However, when the selected image orientation EXIF data is equal to "Rotate 270 CW" the image is not rotated by Glide unless I use the following code:
GlideApp.with(context)
.load(selectedImagePath)
.into(image_view)
This way I pass the selected image Uri instead of a ByteArray, why does this happen?
I attach and example (even here in Stack is not rotated):
Because that interface takes a path and nothing else. Kind of annoying since depending on the api level it is not always available. In some of the newer versions of android it is not easy to get the actual path of the file.image
data for that
The short answer is that the data that interface uses is stored in the file, not in the image data itself. There are many stack overflow links about this:
SO

Picasso does not load image url reference, but loads "http://...."

Picasso doesnt load images if I use imageURL reference but loads when I use actual url e.g. "http://i2.cdn.turner.com/cnnnext/dam/assets/161017171526-cafe-neo-cup-super-169.jpg"
String imageURL = feedItem.getImageUrl();
Picasso.with(getContext()).load(imageURL).resize(600, 0).into(newsImage);
Log.i(LOG_TAG,"Image url is: "+imageURL);
Here is the log output from above
10-20 22:32:00.141 13274-13274/bw.co.fus.print I/NewsFeedAdapter: Image url is: "http://i2.cdn.turner.com/cnnnext/dam/assets/161017171526-cafe-neo-cup-super-169.jpg"
Picasso loads when I use this
Picasso.with(getContext()).load("http://i2.cdn.turner.com/cnnnext/dam/assets/161017171526-cafe-neo-cup-super-169.jpg").resize(600, 0).into(newsImage);
I have tried different resize options, including .fit() and without, also .centercrop(). Also thought it could be null but clearly its not.
Please double check if your feedItem#imageUrl variable doesn't have quotation marks on either side. According to your log output, it has. If you are passing URI to Picasso as a String, it should look like
http://whatever.com/...
and not
"http://whatever.com/..."
Also, use debugger to find out more about what's inside of your model for this specific time (beware, if you are using Realm, it will shown as null, here's why)

Getting error while setting image from parse to imageview

I am using holder to set images from Parse to my image view, below is following code
holder.rank.setText(worldpopulationlist.get(position).getRank());
holder.country.setText(worldpopulationlist.get(position).getCountry());
holder.population.setText(worldpopulationlist.get(position).getPopulation());
holder.flag.setImageResource(Integer.parseInt(worldpopulationlist.get(position).getFlag()));
And below is the error given by android studio
java.lang.NumberFormatException: Invalid int: "http://listview123.herokuapp.com/parse/files/hlkhlkhyuiyemnbbmbackguyweuiyqw/10ad83c5546b993c18be84402e0f2bff_android_1.png"
You can't just say "here is the url of the image, do something"
You have to download the image and set is as the wanted resource.
For a guide look here
Or if you want to use an external library you could, like Azmat said, use Picasso
Picasso.with(context).load(imageURL).into(myImageView);
You can use Picasso to load images from link
Picasso.with(context).load(worldpopulationlist.get(position).getFlag()).into(holder.flag);
p.s: this code works if your worldpopulationlist.get(position).getFlag() returns link for file.

How to load a bitmap into an image-view with Picasso

Yes, I am using Picasso to load a bitmap. The reason is I am decoding URIs in one part of my adapter, and loading bitmaps in another, and I read here that
You should always call Picasso, even if your URL is null. This way it knows that the image view was recycled.
So I tried this....
Bitmap bitMap;
...
Picasso.with(getContext())
.load(bitMap)
.into(imageView);
But I got this error
cannot resolve method 'load(android.graphics.Bitmap)'
You cant put Bitmap for load method of Picasso. You can use only uri , file , url path and int resource id.
If You are downloading image from url then you can do like as below code:
String url = "your_url";
Picasso.with(context).load(url)
.placeholder(R.drawable.any_drawable)
.error(R.drawable.anydrawable).into(your_imageView);
For other resource its same, only load method parameter would gets changed depending on the resource you are using.

Categories

Resources