Im accessing image from gallery or directly from camera in my app. Before sending it to server, I have to be sure that image is not rotated incorrectly. So I'm using function for it to get rotation from gallery or from image taken by camera.
Everything is working as intended without issues, but on Xiaomi Mi A1
it is causing this error:
Caused by: java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.
Camera image error returning from intent(actually what is in Logcat in AndroidStudio):
E/mm-camera: <MCT ><ERROR> 1108: mct_pipeline_decide_hw_wakeup: Couldn't find meta stream
Android version is 9.0. But this is for 100% not version related. I have another Android 9.0 phone where this is working without error. How is it accessing column -1 in gallery when I picked a photo from first row and second column?
Also for camera intent -> there is no exception thrown in console at all -> app will not stop working, only error toast show up( because onActivityResult' intent is
nullbut
resultCodeis
RESULT_OK`).
Rotation functions for gallery and camera:
fun getRotationFromCamera(context: Context, imageFile: Uri): Int {
var rotate = 0
try {
context.contentResolver.notifyChange(imageFile, null)
val exif = ExifInterface(imageFile.path!!)
val orientation = exif.getAttributeInt(
ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_NORMAL
)
when (orientation) {
ExifInterface.ORIENTATION_ROTATE_270 -> rotate = 270
ExifInterface.ORIENTATION_ROTATE_180 -> rotate = 180
ExifInterface.ORIENTATION_ROTATE_90 -> rotate = 90
}
} catch (e: Exception) {
e.printStackTrace()
}
return rotate
}
fun getRotationFromGallery(context: Context, imageUri: Uri): Int {
val columns = arrayOf(MediaStore.Images.Media.ORIENTATION)
val cursor = context.contentResolver.query(imageUri, columns, null, null, null) ?: return 0
cursor.moveToFirst()
val orientationColumnIndex = cursor.getColumnIndex(columns[0])
return cursor.getInt(orientationColumnIndex)
}
Maybe I should include how I access camera and gallery:
private fun openGallery() {
val intent = Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI)
a.startActivityForResult(intent, REQUEST_GALLERY_NODE_MISUSE)
}
private fun openCamera() {
val tempFile = createImageFile()
photoUri = FileProvider.getUriForFile(a, app.packageName + ".fileprovider", tempFile)
val intent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
intent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri)
if (intent.resolveActivity(a.packageManager) != null) {
a.startActivityForResult(intent, REQUEST_CAMERA_NODE_MISUSE)
}
}
UPDATE:
After searching for few hours I've found a solution (only for taking photo by camera). If you open CameraIntent
this Intent is not returning data: Intent
even if you take photo by camera. It will actually add this photo to your empty file made by FileProvider
(in code above it is photoUri
). You have to read photo from this Uri
not from data
sent to onActivityResult
. This is actually weird and it is only like that for Xiaomi
phones. I've tried Samsung
and Razer Phone 2
(both Android 9.0) and data is not null.
Still there is problem why this rotation is not working for both gallery and camera Uri.