[英]How can I determine image size and dimension before downloading the image
I have image URLs for JPEG, GIF and PNG files. 我有JPEG,GIF和PNG文件的图像URL。 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. 在Java中有ImageIO库,但是在AWT库中,我需要Android的东西。
You can get image size easily using URLConnection method getContentLength() 使用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();
Whatever Chrylis says is right. 无论Chrylis说什么都是对的。 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: 编辑:要在Android中获取ImageIO类,请尝试以下操作:
go to project properties * java build pata->Add library * 转到项目属性 * java build pata->添加库 *
and add JRE System Library and click finish . 并添加JRE系统库并单击完成 。 Now you can use java.awt package :) 现在你可以使用java.awt包 :)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.