Getting raster sizes in Android without reading the entire file

I would like to get raster sizes in Android without reading the entire file.

I tried using the recommended inJustDecodeBoundsand custom InputStreamone that registered read()s. Unfortunately, based on this, Android BitmapFactoryseems to be reading a large number of bytes.

Similarly: Java / ImageIO get image sizes without reading the whole file? for Android without using ImageIO.

+5
source share
2 answers

You are right to use inJustDecodeBounds. Set to true and be sure to include options in decoding. It just reads the size of the image.

. .

BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(<pathName>, options);
int imageHeight = options.outHeight;
int imageWidth = options.outWidth;
+11
java.net.URL imageUrl = new java.net.URL("https://yourUrl.com");
HttpURLConnection connection = (HttpURLConnection)imageUrl.openConnection();
connection.connect();
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeStream(connection.getInputStream(), null, options);
int imageWidth = options.outWidth;
int imageHeight = options.outHeight;
0

All Articles