【问题标题】:Is there any way to get the storage size of a Bitmap?有没有办法获取位图的存储大小?
【发布时间】:2011-06-21 11:19:53
【问题描述】:

在我的应用程序中,我正在下载一些图像,我想知道是否有任何方法可以获得我下载的位图的大小。这是一个简单的问题,但我似乎无法通过 Google 找到解决方案。

这里是下载代码:

Bitmap b = BitmapFactory.decodeStream((InputStream) new URL(theImageUrl).getContent());

【问题讨论】:

    标签: android image bitmap download image-size


    【解决方案1】:

    先把Bitmap转换成byte[],然后就可以得到bitmap的大小了

    用你下面的代码试试

    Bitmap bitmap = your bitmap object
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
        byte[] imageInByte = stream.toByteArray();
    long length = imageInByte.length;
    

    【讨论】:

      【解决方案2】:
      File file = new File("infilename");
      
      // Get the number of bytes in the file
      long length = file.length();
      

      【讨论】:

      • 谢谢,但我没有使用 File 类。我正在使用 Bitmap 类。您是否建议将位图转换为文件然后执行此操作?
      • 为什么你需要知道我是如何下载它的?我只是在使用 Bitmap 类。你知道:“位图 b = new Bitmap();”
      • 正如你所说,你正在下载一个文件,那么在下载过程中可以知道大小,你无法使用 Bitmap 来获取大小,或者你必须使用上述方法,不要害怕我不会窃取您的代码:P
      • 我只是害怕逃避问题。我现在把代码放在那里。谢谢。
      • 对不起@NotACleverMan 我不在这里,但我想你得到了答案:D
      【解决方案3】:

      使用b.getByteCount();还是要在下载前查询服务器的大小?

      编辑:此方法仅适用于 API 级别 12。

      【讨论】:

      • 这会返回膨胀的大小,而不是存储大小。
      猜你喜欢
      • 2017-04-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-22
      • 2021-06-12
      • 2019-01-18
      相关资源
      最近更新 更多