0

I have an app that has image upload feature and after uploading the image on onActivityResult() I receive the Uri of the image and turn it into bitmap with the following function :

    private fun uriToBitmap(selectedFileUri: Uri): Bitmap? {
        return try {
            val parcelFileDescriptor: ParcelFileDescriptor =
                requireContext().contentResolver.openFileDescriptor(selectedFileUri, "r")!!
            val fileDescriptor: FileDescriptor = parcelFileDescriptor.fileDescriptor
            val image = BitmapFactory.decodeFileDescriptor(fileDescriptor)
            parcelFileDescriptor.close()
            image
        } catch (e: IOException) {
            e.printStackTrace()
            null
        }
    }

but for some reason my image rotates 90 degrees if it was a portrait image

I have tried using the ExifInterface thing for fixing it and rotating it back with this function :

fun determineImageRotation(imageFile: File, bitmap: Bitmap): Bitmap {
    val exif = ExifInterface(imageFile.absolutePath)
    val orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 0)
    val matrix = Matrix()
    when (orientation) {
        6 -> matrix.postRotate(90f)
        3 -> matrix.postRotate(180f)
        8 -> matrix.postRotate(270f)
    }
    return Bitmap.createBitmap(bitmap, 0, 0, bitmap.width, bitmap.height, matrix, true)
}

but I receive this error :

ExifInterface got an unsupported image format file(ExifInterface supports JPEG and some RAW image formats only) or a corrupted JPEG file to ExifInterface.
    java.io.EOFException

this is how I create a file path for the image :

    @Throws(IOException::class)
    private fun createImageFile(): File {
        // Create an image file name
        val timeStamp: String =
            SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(Date())
        val storageDir = requireContext().getExternalFilesDir(Environment.DIRECTORY_PICTURES)
        return File.createTempFile(
            "PNG_${timeStamp}_", /* prefix */
            ".png", /* suffix */
            storageDir /* directory */
        )
    }

here is an example of a file path that I have for an image :

/storage/emulated/0/Android/data/avedot.app/files/Pictures/PNG_20210622_094219_232594250744276112.png

I assume there is something with the path that is related to the dot in avedot.app whice breaks the ExifInterface function but how can I work around this ?

Thanks in advance !

Yarin Shitrit
  • 297
  • 4
  • 16
  • It is unclear when you create a temp file and why you determine rotation with the File class. You said you had an uri. Well why would you create a temp file yet? Please tell your scenario better. Step by step as at the moment i cant follow you. Edit your post. Dont use comments. – blackapps Jun 22 '21 at 08:23
  • @blackapps I have image capture/upload features in my app, which eventually I want to upload those images to firebase storage with their original size(thats why I turn the images into files). so on `onActivityResult` after image upload/capture I get the imageUri -> I convert the imageUri into bitmap -> I convert the bitmap Into file -> I upload the file to firebase storage. But after converting the imageUri into bitmap the image orientation is changed and I receive wrong orientation on the images. so when I tried to play with the ExifInterface to change the orientation I get error – Yarin Shitrit Jun 22 '21 at 09:30
  • `so on onActivityResult after image upload/capture` You dont show us how you do that. Who tells if rotation is done or not at upload? – blackapps Jun 22 '21 at 10:35

0 Answers0