2013-08-02 34 views

回答

6

您可以輕鬆地使用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(); 
+1

我需要圖像的高度和寬度。 – Mihir

+3

然後,除非服務器有某種方式告訴您尺寸是多少,否則直到您可以自己檢查圖像時纔會發現。 – chrylis

4

無論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包 :)

相關問題