→ 권한체크
- 체크할 권한(Camera) 를 담은 Array 를 함수에 대입
private val RC_PERMISSION = 0
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
binding.btnCamera.setOnClickListener{
val arr = arrayOf(android.Manifest.permission.CAMERA)
checkPermission(arr)
}
}
// 권한검사 함수
private fun checkPermission(requirePermissionList : Array<String>){
val neededPermissionList = ArrayList<String>()
// 전달받은 권한목록중, 허가안된 권한 neededPermissionList에 담기
for (permission in requirePermissionList){
if(ContextCompat.checkSelfPermission(this,permission) != PackageManager.PERMISSION_GRANTED){
neededPermissionList.add(permission)
}
}
// neededPermissionList가 비어있지 않으면, 해당 권한들 허용모달 띄우기
if(neededPermissionList.isNotEmpty()){
val neededPermissionArr = neededPermissionList.toArray(arrayOf<String>())
ActivityCompat.requestPermissions(this, neededPermissionArr, RC_PERMISSION)
}else{
// 비어있으면 Camera 열기
openCamera()
}
}
override fun onRequestPermissionsResult(
requestCode: Int,
permissions: Array<out String>,
grantResults: IntArray
) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
if(requestCode == RC_PERMISSION){
// 권한설정 결과값에 따른 분기처리.
if (ContextCompat.checkSelfPermission(this@MainActivity, Manifest.permission.CAMERA)
!= PackageManager.PERMISSION_GRANTED) {
Log.d(TAG,"권한없음. 권한요청")
} else {
Log.d(TAG,"권한있음. opencamera")
openCamera()
}
}
}
→ 카메라띄우기
- 카메라를 띄우는것은, MediaStore.ACTION_IMAGE_CAPTURE 인텐트를 실행하면 된다
- cameraLauncher은 아래서 설명
private fun openCamera() {
val intent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
cameraLauncher.launch(intent)
}