1

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 1

2

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Is yourFile the file path of the image?
@AdamLee: Yes. Annoyingly, decodeFile() takes a String, not a File.
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.
@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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.