Ask Your Question
3

Is it possible to obtain the width and height of an image file from a URI on an Android device?

asked 2023-05-10 01:09:50 +0000

scrum gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2023-05-10 01:21:02 +0000

ladyg gravatar image

Yes, it is possible to obtain the width and height of an image file from a URI on an Android device. One way to do this is by using the BitmapFactory class in Android. Here is an example:

val uri = // the URI of the image file

val options = BitmapFactory.Options()
options.inJustDecodeBounds = true
BitmapFactory.decodeFileDescriptor(context.contentResolver.openFileDescriptor(uri, "r")?.fileDescriptor, null, options)

val width = options.outWidth
val height = options.outHeight

In this example, we first create a BitmapFactory.Options object and set the inJustDecodeBounds property to true. Then we use the decodeFileDescriptor method of the BitmapFactory class to decode the file and obtain the image dimensions without actually loading the full bitmap into memory. Finally, we can access the width and height values from the BitmapFactory.Options object.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-05-10 01:09:50 +0000

Seen: 13 times

Last updated: May 10 '23