Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

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.