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)
}
}