i can getting the image width through MediaStore.Images.Media
normally
but i need to getting the image width and height from image which selected from dropbox
so currently i have following method to getting image size from dropbox
private void getDropboxIMGSize(Uri uri){
String size = Long.toString(new File(uri.getPath()).length());
return size;
}
but what i actually need are getting the file width and height value
anyone know how to achieve that?please help!
file. length() returns the length of the file in bytes, as described in the Java 7 Documentation: Returns the length, in bytes, of the file denoted by this abstract pathname, or 0L if the file does not exist.
Description. The Image Height is one part of the information that determines a picture's, photo's or other image's dimension. Together with the image width, this value determines the size of an image itself, not the file size. Often, Image Height is given in pixels, e.g. 682 px.
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 );
the solution is use new File(uri.getPath()).getAbsolutePath()
instead of uri.toString()
The accepted answer returns with me a 0 of width/height, by replacing uri.getPath() with uri.getLastPathSegment(), it returns the correct dimensions
public static int[] getImageDimension(Uri uri){
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(new File(uri.getLastPathSegment()).getAbsolutePath(), options);
return new int[]{options.outWidth, options.outHeight};
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With