can i get image file width and height from uri in android? can i get image file width and height from uri in android? android android

can i get image file width and height from uri in android?


private void getDropboxIMGSize(Uri uri){   BitmapFactory.Options options = new BitmapFactory.Options();   options.inJustDecodeBounds = true;   BitmapFactory.decodeFile(new File(uri.getPath()).getAbsolutePath(), options);   int imageHeight = options.outHeight;   int imageWidth = options.outWidth; }

no there is no way. You have to create a Bitmap object. if you use the inJustDecodeBounds flag the bitmap would not be loaded in memory. In fact BitmapFactory.decodeFile will return null. In my example uri is the phisical path to the image


Blackbelt's answer is correct if you have a file uri. However, if you are using the new file providers as in the official camera tutorial, it won't work. This works for that case:

BitmapFactory.Options options = new BitmapFactory.Options();options.inJustDecodeBounds = true;BitmapFactory.decodeStream(        getContext().getContentResolver().openInputStream(mPhotoUri),        null,        options);int imageHeight = options.outHeight;int imageWidth = options.outWidth;


Blackbelt's answer will work most of the time using Options, but I would like to propose another solution or fallback solution by using the ExifInterface. If you have the image URI, you can create the ExifInterface using the full path, no need for Bitmap object nor BitmapFactory.Options.

ex.

int width = exif.getAttributeInt( ExifInterface.TAG_IMAGE_WIDTH, defaultValue );int height = exif.getAttributeInt( ExifInterface.TAG_IMAGE_LENGTH, defaultValue );