I am loading images from dynamic sources and loading them in my app. However sometimes the images are so small and looks bad in my app. What I want to do is get image size and if it is smaller than 5x5, don't show the ImageView at all.
How to achieve this?
When I use sizeReadyCallback, it returns the size of ImageView instead of image. When I use request listener it returns 0,0.
Glide.with(getContext()).load(imageUrl).listener(new RequestListener<String, GlideDrawable>() {
@Override
public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
return false;
}
@Override
public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
//This returns 0,0
Log.e("TAG","_width: " + resource.getBounds().width() + " _height:" +resource.getBounds().height());
return false;
}
}).into(ivImage).getSize(new SizeReadyCallback() {
@Override
public void onSizeReady(int width, int height) {
//This returns size of imageview.
Log.e("TAG","width: " + width + " height: " + height);
}
});
Add a template column in the Glide data editor, containing the image or url of the default picture. Then, add an if/then/else column in the data editor : if “user image” is empty then “default image” else “user image”.
Add an explicit width or height to the ImageView by setting layout_width=500dp in the layout file. Call . override(width, height) during the Glide load and explicitly set a width or height for the image such as: GlideApp.
On PC right click your image, click properties, go to details tab, and look in the image section. On a Mac, double tap the trackpad, select "get info" option, and you will see your image size. On android go to photos, select your photo and click the ... in the top right. Scroll to bottom of page to find image size.
Update:
A better solution has been provided by @TWiStErRob in comments: better solution
For Glide v4:
Glide.with(getContext().getApplicationContext())
.asBitmap()
.load(path)
.into(new SimpleTarget<Bitmap>() {
@Override
public void onResourceReady(Bitmap bitmap,
Transition<? super Bitmap> transition) {
int w = bitmap.getWidth();
int h = bitmap.getHeight()
mImageView.setImageBitmap(bitmap);
}
});
The point is getting the bitmap before set into an ImageView
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With