简体   繁体   中英

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.

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

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 :)

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