Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get original image size by using Glide on Android?

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);
        }
    });
like image 722
Swifty Swift Avatar asked Dec 15 '17 06:12

Swifty Swift


People also ask

How do I change the default picture in Glide Android?

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”.

How do I change the height and width of a picture in Glide?

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.

How do I check image size on Android?

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.


1 Answers

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.

like image 199
thundertrick Avatar answered Oct 21 '22 16:10

thundertrick