Issue
I am checking if my application have access to camera and to write external storage, but the first time when I am asked for permission even after giving them the app still return false, Here is my code
private fun getPermission(): Boolean {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if( (checkSelfPermission(android.Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED )&&
checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE) ==PackageManager.PERMISSION_GRANTED) {
return true
} else {
requestPermissions(listOf(CAMERA, WRITE_EXTERNAL_STORAGE).toTypedArray(), 1)
}
}
return false
}
so if, I use this function getPermission it will first time return false (on screen I am allowing my application to access camera and storage ) and on next turn (I go back to home activity and again open this Camera activity) this time I am not asked for permissions and my application starts using camera.
Solution
As described in @Marko answer all I was missing was onRequestPermissionsResult callback, here is the code:
//the is a overridden method
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
if(requestCode==1){
if(grantResults[0]==PERMISSION_DENIED && grantResults[1]== PERMISSION_DENIED){
AlertDialogBox().createBuilder(this,"Permission","Camera and Storage permission denied","ok")
}else if(grantResults[1]== PERMISSION_DENIED){
AlertDialogBox().createBuilder(this,"Permission","Storage permission denied","ok")
}else if(grantResults[0]== PERMISSION_DENIED){
AlertDialogBox().createBuilder(this,"Permission","Camera permission denied","ok")
}
}
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
}
Answered By - Abdul Rahman Shamair
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.