Skip to content

Instantly share code, notes, and snippets.

@imandaliya
Created October 20, 2021 07:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save imandaliya/b771c8f959eee6267fb68fb31f6792ce to your computer and use it in GitHub Desktop.
Save imandaliya/b771c8f959eee6267fb68fb31f6792ce to your computer and use it in GitHub Desktop.
fun Uri.calculateImageDimens(context: Context): String? {
try {
context.contentResolver.openInputStream(this)?.let {
val exif = ExifInterface(it)
val width = exif.getAttributeInt(ExifInterface.TAG_IMAGE_WIDTH, -1)
if (width > 0) return "$width x $width"
}
context.contentResolver.openFileDescriptor(this, "r")?.let { fd ->
val options: BitmapFactory.Options = BitmapFactory.Options()
options.inJustDecodeBounds = true
BitmapFactory.decodeFileDescriptor(fd.fileDescriptor, null, options)
return "${options.outWidth} x ${options.outHeight}"
}
} catch (e: IOException) {
e.printStackTrace()
} catch (e: Exception) {
e.printStackTrace()
}
return null
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment