记:Android 26版本以上相机和裁剪的问题

高版本跳转相机:

val intent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)

 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {

        file = File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM), img_name)

        intent.putExtra(MediaStore.EXTRA_OUTPUT, FileProvider.getUriForFile(activity,                 "packagename.FileProvider", file!!))

        intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)

        intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION)

} else  {

        intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file))

}

高版本裁剪:

        val intent = Intent("com.android.camera.action.CROP")

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {

            val outPutUri = uri.fromFile(file)

            intent.setDataAndType(uri, "image/*")

            intent.putExtra(MediaStore.EXTRA_OUTPUT, outPutUri)

            intent.putExtra("noFaceDetection", false)

            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)

            intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION)

} else  {

            val outPutUri = Uri.fromFile(file)

            if (Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {

            val url = GetImagePath().getPath(activity, uri)//这个方法是处理4.4以上图片返回的Uri对象不同的处理方法

        intent.setDataAndType(Uri.fromFile(File(url)), "image/*")

} else {

        intent.setDataAndType(uri, "image/*")

}

        intent.putExtra(MediaStore.EXTRA_OUTPUT, outPutUri)

}

        // 设置裁剪

        intent.putExtra("crop", "true")

        // aspectX aspectY 是宽高的比例

        intent.putExtra("aspectX", 1)

        intent.putExtra("aspectY", 1)

        // outputX outputY 是裁剪图片宽高

        intent.putExtra("outputX", 150)

        intent.putExtra("outputY", 150)

你可能感兴趣的:(记:Android 26版本以上相机和裁剪的问题)