Glide: How can i get the size of the image ?

Created on 19 Jan 2016  路  8Comments  路  Source: bumptech/glide

when i use glide to load image, i use .override(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL) to get the original image.

my code is :

Glide.with(mContext)
    .load(url)
    .override(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL)
    .crossFade()
    .into(new SimpleTarget<GlideDrawable>() {
        @Override
        public void onResourceReady(GlideDrawable glideDrawable, GlideAnimation<? super GlideDrawable> glideAnimation) {
            int width = glideDrawable.getIntrinsicWidth();
            int height = glideDrawable.getIntrinsicHeight();
            viewHolder.image1.setImageDrawable(glideDrawable);
        }
    });

but the size is incorrect.
then how can i get the correct size of the image?

question

Most helpful comment

@thundertrick

The best solution might be create a custom ModelLoader or sth like that, which can make full use of the optimization inside Glide.

Exactly what's in #781. Note that loading the bitmap to get the size is very inefficient. Think about image formats. Loading a 3MB JPEG decoded as 20MB of Bitmap just to read the size and then throw away; that's a lot of unnecessary Fourier transformations. The first 100-ish bytes contains the size of the full image (in header / EXIF), so it's a pretty low-resource solution to read that with inJustDecodeBounds; and file format differences are handled for you by Android.

@dy60420667 if you add .override(screenWidth,screenHeight) then getIntrinsicWidth will give you the same numbers.

All 8 comments

See #781.

Ok, Thank you very much! @TWiStErRob

Glide.with(mContext)
    .load(url)
    .override(screenWidth,screenHeight)
    .crossFade()
    .into(new SimpleTarget<GlideDrawable>() {
        @Override
        public void onResourceReady(GlideDrawable glideDrawable, GlideAnimation<? super GlideDrawable> glideAnimation) {
            int width = glideDrawable.getIntrinsicWidth();
            int height = glideDrawable.getIntrinsicHeight();
            viewHolder.image1.setImageDrawable(glideDrawable);
        }
    });

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

@thundertrick where your variable w and h had been used

@thundertrick where your variable w and h had been used

w and h are the size of the original bitmap.

However, this might not be the best solution, because the original bitmap is set into the view, which might cost too much space. See more on my StackOverFlow answer, the first comment point out this problem.

The best solution might be create a custom ModelLoader or sth like that, which can make full use of the optimization inside Glide.

Hope it helps.

@thundertrick

The best solution might be create a custom ModelLoader or sth like that, which can make full use of the optimization inside Glide.

Exactly what's in #781. Note that loading the bitmap to get the size is very inefficient. Think about image formats. Loading a 3MB JPEG decoded as 20MB of Bitmap just to read the size and then throw away; that's a lot of unnecessary Fourier transformations. The first 100-ish bytes contains the size of the full image (in header / EXIF), so it's a pretty low-resource solution to read that with inJustDecodeBounds; and file format differences are handled for you by Android.

@dy60420667 if you add .override(screenWidth,screenHeight) then getIntrinsicWidth will give you the same numbers.

@thundertrick

Was this page helpful?
0 / 5 - 0 ratings

Related issues

MrFuFuFu picture MrFuFuFu  路  3Comments

r4m1n picture r4m1n  路  3Comments

Tryking picture Tryking  路  3Comments

sant527 picture sant527  路  3Comments

piedpiperlol picture piedpiperlol  路  3Comments