高版本跳轉相機:
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)