如何在下载图像之前确定图像大小和尺寸

Mih*_*hir 2 java url android image bitmap

我有JPEG,GIF和PNG文件的图像URL.我想检查这些网址中的图片是否小于特定尺寸.

在此基础上,我想下载图像.

在Java中有ImageIO库,但是在AWT库中,我需要Android的东西.

Muk*_*med 8

使用URLConnection方法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(); 
Run Code Online (Sandbox Code Playgroud)

  • 然后,除非服务器有某种方式告诉您大小是什么,否则在您自己检查图像之前不会发现. (3认同)