如何计算图像大小 natively/internally?

How is an image size calculated natively/internally?

当我们将 OptionsinJustDecodeBounds=true 传递给 BitmapFactory.decodeStream 方法时,它 解码 只有 文件大小(高和宽).
android如何计算尺寸(高和宽)?
它会下载完整文件然后计算大小吗?
InputStream 中有一个方法 avaliable(),但它 returns 只是估计的大小。
我想知道它的内部工作原理。

从您的 java 应用程序调用 decodeStream 方法将调用对基于 InputStream 类型的本机解码实现的调用。参见 public static Bitmap decodeStream(InputStream is, Rect outPadding, Options opts) here

选项被传递给带有签名 static jobject doDecode(JNIEnv* env, SkStreamRewindable* stream, jobject padding, jobject options) 的本地解码器实现进行解码,see here.

Inspecting the code further, you will see that when the option inJustDecodeBounds=true, the boolean onlyDecodeSize is set to true in the doDecode method.然后,解码方法将处理流,直到所有大小的元数据都被提取出来。此时它将 return 一个空指针,使您可以访问图像元数据而不是完整图像。