【问题标题】:How can I determine image size and dimension before downloading the image如何在下载图像之前确定图像大小和尺寸
【发布时间】:2013-08-03 06:54:54
【问题描述】:

我有 JPEG、GIF 和 PNG 文件的图像 URL。我想检查这些 URL 中的图像是否小于特定大小。

在此基础上,我想下载图像。

Java 中有 ImageIO 库,但它在 AWT 库中,我需要一些用于 Android 的东西。

【问题讨论】:

    标签: java android image url bitmap


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      您可以使用 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(); 
      

      【讨论】:

      • 我需要图片的高度和宽度。
      • 那么除非服务器有某种方式告诉你尺寸是多少,否则在你自己检查图像之前你不会发现。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多