我有用於JPEG,GIF和PNG文件的圖像URL。我想檢查這些網址中的圖片是否小於特定尺寸。如何在下載圖像之前確定圖像大小和尺寸
在此基礎上我想下載圖像。
在Java中有ImageIO庫,但那是在AWT庫中,我需要Android的東西。
我有用於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;
}
編輯: 要獲得的ImageIO類在你的Android嘗試以下操作:
去項目屬性 * java build pata->添加庫 *
並加上JRE系統庫並點擊完成。現在你可以使用java.awt包 :)
我需要圖像的高度和寬度。 – Mihir
然後,除非服務器有某種方式告訴您尺寸是多少,否則直到您可以自己檢查圖像時纔會發現。 – chrylis