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

jk jk picture jk jk · May 8, 2013 · Viewed 40.5k times · Source

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!

Answer

Blackbelt picture Blackbelt · May 8, 2013
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