0

I have the image rotate problem. All the solutions I have found are outdated. The closest I have got is following How to get uri of captured photo? to get the uri. If I include the line takePicture.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, uri) then I do not get resultCode == Activity.RESULT_OK.
If I run with the line in then I get a crash at val ei = ExifInterface(uri.toString()). Also .getExternalStorageDirectory() is deprecated and I can't find what I am supposed to be using.

class SelImageDialogFragment: DialogFragment() {

    var useCamera: Int = 0
    var useGallery: Int = 0
    private val options = arrayOf("Take Photo", "Choose from Gallery", "Cancel")

    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        return activity?.let {
            val builder = AlertDialog.Builder(it)
            builder.setTitle("Choose profile picture")
            builder.setItems(options) { dialog, item ->
                if (options[item] == options[0]) {
                    val takePicture = Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE)
                    val file = File(Environment.getExternalStorageDirectory(), "MyPhoto.jpg")
                    val uri = FileProvider.getUriForFile(MyApp.applicationContext(), MyApp.applicationContext().packageName + ".provider", file)
                    takePicture.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, uri)
                    activity!!.startActivityForResult(takePicture, useCamera)
                } else if (options[item] == options[1]) {
                    val pickPhoto = Intent(
                        Intent.ACTION_PICK,
                        android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI
                    )
                    activity!!.startActivityForResult(pickPhoto, useGallery)
                } else if (options[item] == options[2]) {
                    dialog.dismiss()
                }
            }
            builder.create()
        } ?: throw IllegalStateException("Activity cannot be null")
    }
}


if (resultCode == Activity.RESULT_OK && data != null) {
                    val imageBitmap = data.extras?.get("data")
                    val file = File(Environment.getExternalStorageDirectory(), "MyPhoto.jpg")
                    val uri = FileProvider.getUriForFile(this, this.applicationContext.packageName + ".provider", file)
                    val ei = ExifInterface(uri.toString())
                    val orientation = ei.getAttribute(ExifInterface.TAG_ORIENTATION)
                    when (orientation) {
                        ExifInterface.ORIENTATION_ROTATE_90.toString() -> {
                            println("NOTE orientation 90 out")
                        }
                        ExifInterface.ORIENTATION_ROTATE_180.toString() -> {
                            println("NOTE orientation 180 out")
                        }
                        ExifInterface.ORIENTATION_ROTATE_270.toString() -> {
                            println("NOTE orientation 270 out")
                        }
                        ExifInterface.ORIENTATION_NORMAL.toString() -> {
                            println("NOTE orientation normal")
                        }
                    }

                    binding.profileImg.setImageBitmap(imageBitmap as Bitmap?)
                    if (imageBitmap != null) {
                        encodeBitmapAndSaveToFirebase(imageBitmap)
                    }
                }
Peter
  • 79
  • 7
  • Post the logcat so we know why your app crashes. – blackapps May 25 '22 at 17:21
  • If you have a File instance then you do not need to make an uri for it to present to ExifInterface. ExifInterface can handle the File instance directly. Also use File.exists() before using ExifInterface. – blackapps May 25 '22 at 17:23
  • How can you talk about a rotate issue when your app crashes? – blackapps May 25 '22 at 17:24
  • Refer to this answer I posted yesterday: https://stackoverflow.com/questions/72271736/take-a-photo-and-saving-it-in-full-size/72362209#72362209 – Abu bakar May 25 '22 at 20:07

0 Answers0