Mihir
Mihir

Reputation: 2084

How can I determine image size and dimension before downloading the image

I have image URLs for JPEG, GIF and PNG files. I want to check whether the images in those URLs are smaller than a specific size or not.

On the basis of that I want to download the image.

There ImageIO library in java for that but that's in the AWT library, I need something for Android.

Upvotes: 2

Views: 8118

Answers (2)

TheLittleNaruto
TheLittleNaruto

Reputation: 8473

Whatever Chrylis says is right. You can do it only after downloading . First download your image file and save it to a specific path say it folder.

And from there, read it and get its height and width using below code:

BufferedImage readImage = null;

try {
    readImage = ImageIO.read(new File(folder);
    int h = readImage.getHeight();
    int w = readImage.getWidth();
} catch (Exception e) {
    readImage = null;
}

EDIT: To get ImageIO class in your android try the following:

go to project properties *java build pata->Add library*

and add JRE System Library and click finish. Now you can use java.awt package :)

Upvotes: 4

Mukhtiar Ahmed
Mukhtiar Ahmed

Reputation: 631

You can get image size easily using URLConnection method getContentLength()

 URL url = new URL("https://www.google.com.pk/images/srpr/logo4w.png");
 URLConnection conn = url.openConnection();

  // now you get the content length
 int contentLength = conn.getContentLength();
 // you can check size here using contentLength

 InputStream in = conn.getInputStream();
 BufferedImage    image = ImageIO.read(in);
  // you can get size  dimesion      
  int width          = image.getWidth();
  int height         = image.getHeight(); 

Upvotes: 9

Related Questions