Created
October 20, 2021 07:32
-
-
Save imandaliya/b771c8f959eee6267fb68fb31f6792ce to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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