0

I have the following method to rotate image if needed and it is an extension method for Uri class

suspend fun Uri.rotateImageIfRequired(ctx: Context): Uri {

    Log.i("AMIRA3333" , "uri2 : " + this)

    val originalBitmap = MediaStore.Images.Media.getBitmap(ctx.contentResolver, this)
    val originalImagePath = this.getPath(ctx)

    val exifInterface = ExifInterface(originalImagePath)
    val orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL)

    return when (orientation) {
        ExifInterface.ORIENTATION_ROTATE_90 -> TransformationUtils.rotateImage(originalBitmap, 90)
        ExifInterface.ORIENTATION_ROTATE_180 -> TransformationUtils.rotateImage(originalBitmap, 180)
        ExifInterface.ORIENTATION_ROTATE_270 -> TransformationUtils.rotateImage(originalBitmap, 270)
        else -> originalBitmap
    }.let {
        GlobalScope.async {
            File(originalImagePath).deleteOnExit()
            Uri.parse(
                MediaStore.Images.Media.insertImage(
                    ctx.contentResolver,
                    it,
                    System.currentTimeMillis().toString(),
                    null
                )
            )
        }.await()
    }
}

and this is an example for my uri : file:///storage/emulated/0/DCIM/IMG_20190219_072926.jpg

it is always return null

Zoe
  • 27,060
  • 21
  • 118
  • 148
Amira Elsayed Ismail
  • 9,216
  • 30
  • 92
  • 175

0 Answers0