2013-08-02 36 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包 :)

相关问题