简体   繁体   中英

How to get image width and height from a network url

I am working on an android app and using REST APIs in it. I am receiving image urls from server and I want to show these images according to the original height of the image using image url.

How can I get the width and height of image using network image url. I have already tried getting bitmap from the string url but for this I want to perform this in background thread which leads the image loading very slow.

Is there any way I can get the image width and height from image url and can show the image in imageview according to the width and height.

Please help me if anyone know about this.

Thanks a lot in advanced.

URL url = new URL("url/image.jpg");
Bitmap bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream());

for width:

bmp.getWidth(); 

for height:

bmp.getHeight(); 

for setting in imageview:

ImageView.setImageBitmap(bmp);

Further Reference:

https://developer.android.com/reference/android/graphics/Bitmap.html#getHeight%28%29

/* your library */
implementation 'com.github.bumptech.glide:glide:4.11.0'    
annotationProcessor 'com.github.bumptech.glide:compiler:4.11.0'

/* your code */

    Glide.with(this)
            .asBitmap()      //get hieght and width 
            .load("link image")
            .into(new SimpleTarget<Bitmap>() {
                @Override
                public void onResourceReady(@NonNull Bitmap resource, @Nullable 
                    Transition<? super Bitmap> transition) {
                    Log.d("TAG", "onResourceReady: "+resource.getWidth()+"    
                    "+resource.getHeight());
                }
            });

    //////////////////////////////////////

    Glide.with(this)
            .asFile()     // get size Image 
            .load("link image")
            .into(new SimpleTarget<File>() {
                @Override
                public void onResourceReady(@NonNull File resource, @Nullable 
                    Transition<? super File> transition) {
                    fab_full.setLabelText(""+resource.length());
                }
            });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM