[英]How can I determine image size and dimension before downloading the image
我有JPEG,GIF和PNG文件的圖像URL。 我想檢查這些網址中的圖片是否小於特定尺寸。
在此基礎上,我想下載圖像。
在Java中有ImageIO庫,但是在AWT庫中,我需要Android的東西。
使用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();
無論Chrylis說什么都是對的。 您只能在下載后才能這樣做。 首先下載您的圖像文件並將其保存到特定路徑說它文件夾 。
然后從那里讀取它並使用下面的代碼獲得它的高度和寬度:
BufferedImage readImage = null;
try {
readImage = ImageIO.read(new File(folder);
int h = readImage.getHeight();
int w = readImage.getWidth();
} catch (Exception e) {
readImage = null;
}
編輯:要在Android中獲取ImageIO類,請嘗試以下操作:
轉到項目屬性 * java build pata->添加庫 *
並添加JRE系統庫並單擊完成 。 現在你可以使用java.awt包 :)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.