Android 使用ucrop中出现的Bounds for bitmap could not be retrieved from Uri报错

使用第三方开源ucrop 裁剪图片时,将相机拍摄完成后返回的uri,直接放入UCrop.of(),会出现Bounds for bitmap could not be retrieved from Uri 报错

解决方法(kotlin1.6):

使用 registerForActivityResult(ActivityResultContracts.TakePicturePreview()) {}

该方法会直接返回bitmap 将bitmap 另行保存后,再将对应的uri放入UCrop.of()就可以了

代码:

//拍照
    private val takePhoto =
        registerForActivityResult(ActivityResultContracts.TakePicturePreview()) {

            if (it !=null) {
                val fileName="takePhoto${System.currentTimeMillis()}.jpg"
                val filePath=mContext.getExternalFilesDir("PicturesCache")!!.path
                FileUtil.saveBitmap(it,fileName,filePath)
                startCrop(Uri.fromFile(File(filePath,fileName)))
                }
        }

    takePhoto.launch(null)

猜你喜欢

转载自blog.csdn.net/oxygen112/article/details/126988963