无法从CursorWindow中读取行。从图库旋转图像时出错

时间:2019-07-23 05:36:51

标签: android kotlin android-camera android-gallery

我从图库中访问图像,或者直接从我的应用程序中的相机访问图像。在将其发送到服务器之前,必须确保图像旋转不正确。因此,我正在使用该函数从画廊或相机拍摄的图像中获取旋转。

一切正常,没有问题,但是在Xiaomi Mi A1上却导致此错误:

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.

从意图返回的相机图像错误(实际上是AndroidStudio中的Logcat中的错误):

E/mm-camera: <MCT   ><ERROR> 1108: mct_pipeline_decide_hw_wakeup: Couldn't find meta stream

Android版本是9.0。但这是100%与版本无关的。我还有另一部Android 9.0手机,可以正常使用。当我从第一行和第二列中选择一张照片时,如何访问画廊的-1列?

出于相机意图->根本没有在控制台中引发任何异常->应用将不会停止工作,只会显示错误提示(因为onActivityResult' intent isbut结果代码{{ 1}} RESULT_OK`。

画廊和照相机的旋转功能:

is

也许我应该包括如何访问相机和画廊:

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

更新:

搜索了几个小时后,我找到了解决方案(仅适用于通过相机拍摄照片)。如果您打开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) } } ,即使您使用相机拍照,此Intent也不会返回CameraIntent。它实际上会将这张照片添加到data: Intent制作的空白文件中(在上面的代码中为FileProvider)。您必须从此photoUri中读取照片,而不要从发送至Uri的{​​{1}}中读取照片。这实际上很奇怪,仅与data手机的情况类似。我已经尝试过onActivityResultXiaomi(都是Android 9.0),并且数据不为空

仍然存在一个问题,为什么这种旋转对于画廊和相机Uri均不起作用。

0 个答案:

没有答案
相关问题