Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting a bitmap's dimensions in Android without reading the entire file

Tags:

android

I would like to get a bitmap's dimensions in Android without reading the entire file.

I tried using the recommended inJustDecodeBounds and a custom InputStream that logged the read()s. Unfortunately based on this, the Android BitmapFactory appears to read a large amount of bytes.

Similar to: Java/ImageIO getting image dimensions without reading the entire file? for Android, without using ImageIO.

like image 942
mparaz Avatar asked Aug 18 '12 12:08

mparaz


1 Answers

You are right to use inJustDecodeBounds. Set it to true and make sure to include Options in decode. This just reads the image size.

There is no need to read a stream. Just specify the path in the decode.

BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(<pathName>, options);
int imageHeight = options.outHeight;
int imageWidth = options.outWidth;
like image 127
IAmGroot Avatar answered Sep 19 '22 03:09

IAmGroot