0

When we pass Options with inJustDecodeBounds=true to the BitmapFactory.decodeStream method, it decodes only the file size(Height and width).
How does android calculate the size (height and width)?
Does it download the full file and then calculates the size?
In InputStream there a method avaliable(), but it returns only the estimated size.
I want to know the internal working of this.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Ankur Samarya
  • 219
  • 1
  • 3
  • 15

1 Answers1

0

Calling the decodeStream method from your java application will invoke a call to the native decoding implementation based on the InputStream type. See public static Bitmap decodeStream(InputStream is, Rect outPadding, Options opts) here.

The options are passed on to the native decoder implementation with signature static jobject doDecode(JNIEnv* env, SkStreamRewindable* stream, jobject padding, jobject options) for decoding, 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. The decode method will then process the stream up until all size meta data has been extract. At this point it will return a null pointer, giving you access to the image meta data but not the full image.

sparkitny
  • 1,503
  • 4
  • 18
  • 23
  • where is the code for extracting the meta data from stream ? – Ankur Samarya Dec 25 '17 at 17:42
  • From line 298 in the cpp file. It uses the codec to process the stream, width and height are stored in an SkISize object. The codec is initialised on 283 with a pointer to the input stream. – sparkitny Dec 25 '17 at 20:34
  • Ok got it, since it is a image file(png/jpeg/etc.) so stream would have image header that contains information about size. Earlier i considered stream as a normal stream so i was wondered how can size will be calculated via input stream. – Ankur Samarya Dec 26 '17 at 06:41
  • Yes that is correct. Infact, if you were curious, the Bitmap specification defines the Bitmap file structure, which includes the file header and bitmap header at the start of the stream: http://www.fileformat.info/format/bmp/egff.htm. If you are satisfied with my answer could you please remember to assign the bounty. Thanks :) – sparkitny Dec 27 '17 at 04:27