Closed ghost closed 7 months ago
This issue is stale because it has been open 20 days with no activity. Remove stale label or comment or this will be closed in 4 days.
I was facing same error then it was solved by code below
create new class and paste this code,
import android.content.Context
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.Matrix
import android.media.ExifInterface
import android.net.Uri
import android.os.Build
import java.io.IOException
@Throws(IOException::class)
public fun hanleImageRotation(context: Context, selectedImage: Uri?): Bitmap? {
val MAX_HEIGHT = 1024
val MAX_WIDTH = 1024
val options = BitmapFactory.Options()
options.inJustDecodeBounds = true
var imageStream = context.contentResolver.openInputStream(selectedImage!!)
BitmapFactory.decodeStream(imageStream, null, options)
imageStream!!.close()
options.inSampleSize = calculateInSampleSize(options, MAX_WIDTH, MAX_HEIGHT)
options.inJustDecodeBounds = false
imageStream = context.contentResolver.openInputStream(selectedImage)
var img = BitmapFactory.decodeStream(imageStream, null, options)
img = rotateImageIfRequired(context, img!!, selectedImage)
return img
}
private fun calculateInSampleSize(
options: BitmapFactory.Options,
reqWidth: Int, reqHeight: Int
): Int {
val height = options.outHeight
val width = options.outWidth
var inSampleSize = 1
if (height > reqHeight || width > reqWidth) {
val heightRatio = Math.round(height.toFloat() / reqHeight.toFloat())
val widthRatio = Math.round(width.toFloat() / reqWidth.toFloat())
inSampleSize = if (heightRatio < widthRatio) heightRatio else widthRatio
val totalPixels = (width * height).toFloat()
val totalReqPixelsCap = (reqWidth * reqHeight * 2).toFloat()
while (totalPixels / (inSampleSize * inSampleSize) > totalReqPixelsCap) {
inSampleSize++
}
}
return inSampleSize
}
@Throws(IOException::class)
private fun rotateImageIfRequired(context: Context, img: Bitmap, selectedImage: Uri): Bitmap? {
val input = context.contentResolver.openInputStream(selectedImage)!!
val ei: ExifInterface
if (Build.VERSION.SDK_INT > 23) ei = ExifInterface(input) else ei = ExifInterface(selectedImage.path!!)
val orientation: Int = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL)
return when (orientation) {
ExifInterface.ORIENTATION_ROTATE_90 -> rotateImage(img, 90)
ExifInterface.ORIENTATION_ROTATE_180 -> rotateImage(img, 180)
ExifInterface.ORIENTATION_ROTATE_270 -> rotateImage(img, 270)
else -> img
}
}
private fun rotateImage(img: Bitmap, degree: Int): Bitmap? {
val matrix = Matrix()
matrix.postRotate(degree.toFloat())
val rotatedImg = Bitmap.createBitmap(img, 0, 0, img.width, img.height, matrix, true)
img.recycle()
return rotatedImg
}
And in EditImageActivity change this with onActivityResult()
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (resultCode == RESULT_OK) {
when (requestCode) {
CAMERA_REQUEST -> {
mPhotoEditor.clearAllViews()
val photo = data?.extras?.get("data") as Bitmap?
mPhotoEditorView.source.setImageBitmap(photo)
}
PICK_REQUEST -> try {
mPhotoEditor.clearAllViews()
val uri = data?.data
val bitmap = MediaStore.Images.Media.getBitmap(
contentResolver, uri
)
mPhotoEditorView.source.setImageBitmap(hanleImageRotation(this, uri))
} catch (e: IOException) {
e.printStackTrace()
}
}
}
}
This issue is stale because it has been open 20 days with no activity. Remove stale label or comment or this will be closed in 4 days.
This issue was closed because it has been stalled for 5 days with no activity.
Describe the bug When I open my camera photos with this app they are being rotated upside down, then app doesn't let me to rotate them back, it's fine with snapshot
Expected behavior I would expect the app not to rotate photos
Smartphone Info:
Additional context Add any other context about the problem here.