Given an image stored in a File object, how would I get the width and height of that image without using the javax.imageio module, since Android does not allow you to use that module?
1 Answer
Use something like this:
BitmapFactory.Options bmpOptions = new BitmapFactory.Options(); bmpOptions.inJustDecodeBounds = true; BitmapFactory.decodeFile(yourFile, bmpOptions); bmpOptions will have outWidth and outHeight with your image dimensions.
4 Comments
Adam Lee
Is yourFile the file path of the image?
CommonsWare
@AdamLee: Yes. Annoyingly,
decodeFile() takes a String, not a File.Adam Lee
Is there any way to get the width and height from a URI instead? It is extremely difficult for me to get the image's absolute file path since Android does not seem to allow you to get the absolute file path from a URI.
CommonsWare
@AdamLee: "Is there any way to get the width and height from a URI instead?" -- if by "URI" you mean
Uri that you get back from something like ACTION_OPEN_DOCUMENT, use BitmapFactory.decodeStream(). You can get an InputStream on the content by passing the Uri to openInputStream() on a ContentResolver. "Android does not seem to allow you to get the absolute file path from a URI" -- that is because a Uri does not necessarily point to a file on the filesystem that your app has rights to access.