Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android: get image dimensions without opening it

Tags:

android

I want to get width and height (in pixels) of images which are stored on the sdcard, before loading them into RAM. I need to know the size, so I can downsample them accordingly when loading them. Without downsampling them I get an OutOfMemoryException.

Anyone knows how to get dimensions of image files?

like image 410
stoefln Avatar asked Jun 20 '12 21:06

stoefln


People also ask

How do I find the dimensions of an image?

You can also right-click on an image & choose properties from the drop-down menu. A new window will appear with several tabs. You'll click the details tab, and there you'll find you image size and dimensions.

How do I find out the size of a photo on my phone?

File Img = new File(selectedImage. getPath()); int length = Img. length(); This returns length in Bytes.


2 Answers

Pass the option to just decode the bounds to the factory:

BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true;  //Returns null, sizes are in the options variable BitmapFactory.decodeFile("/sdcard/image.png", options); int width = options.outWidth; int height = options.outHeight; //If you want, the MIME type will also be decoded (if possible) String type = options.outMimeType; 
like image 121
devunwired Avatar answered Sep 21 '22 23:09

devunwired


Actually, there is another way to solve this problem. Using the below way, we can avoid the trouble with file and URI.

BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; ParcelFileDescriptor fd = mContext.getContentResolver().openFileDescriptor(u, "r"); // u is your Uri BitmapFactory.decodeFileDescriptor(fd.getFileDescriptor(), null, options); 
like image 31
Tinh Duong Avatar answered Sep 22 '22 23:09

Tinh Duong