Prithniraj Nicyone
Prithniraj Nicyone

Reputation: 5121

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.

Upvotes: 1

Views: 10367

Answers (2)

Mhammed Khaled
Mhammed Khaled

Reputation: 89

/* 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());
                }
            });

Upvotes: 0

Teerath Kumar
Teerath Kumar

Reputation: 488

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

Upvotes: 7

Related Questions