Home > Enterprise >  how to get permission result in kotlin
how to get permission result in kotlin

Time:03-14

I am trying to use a camera on my android. the user will be prompt for the permission of the camera. previously I used startActivityForResult and onRequestPermissionRequest for them. recently I found out that they are deprecated, so I'm trying out with registerForActivity. I managed to change to startActivity but I'm stuck at the permission request. I am wondering do I have to create another permissionlauncher or can I do the permission inside my resultlauncher.

    companion object{
        private const val CAMERA_PERMISSION_CODE = 1
        private const val CAMERA_REQUEST_CODE = 2 
    }
val checkpermission = Manifest.permission.CAMERA

        var resultLauncher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()){ result ->
                if (result.resultCode == Activity.RESULT_OK) {
                    val data: Intent? = result.data
                    val DP: Bitmap = data!!.extras!!.get("data") as Bitmap
                    val image = findViewById<ImageView>(R.id.imageButtonVerifyPhoto)
                    image.setImageBitmap(DP)
                }

            }

        val permissionLauncher = registerForActivityResult(ActivityResultContracts.RequestPermission()){
                isGranted ->
            if(isGranted){
                val intent = Intent(MediaStore.ACTION_IMAGE_CAPTURE) 
                resultLauncher.launch((intent))
                Toast.makeText(this,"Permission is tested", Toast.LENGTH_SHORT).show()
            }else{
                Toast.makeText(this,"Permission is denied",Toast.LENGTH_SHORT).show()
            }

        }
        var cameraButton = findViewById<Button>(R.id.buttonRetakePhoto) // can change later

        cameraButton.setOnClickListener {
            if(ContextCompat.checkSelfPermission(
                    this,
                    checkpermission
                ) == PackageManager.PERMISSION_GRANTED
            ){
                val intent = Intent(MediaStore.ACTION_IMAGE_CAPTURE) 
                resultLauncher.launch(intent)

            }else{
                permissionLauncher.launch(checkpermission)
            }
        }

    }

below is my previous code for the onRequestPermission

    override fun onRequestPermissionsResult(
        requestCode: Int,
        permissions: Array<out String>,
        grantResults: IntArray
    ) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        if(requestCode == CAMERA_REQUEST_CODE){
            if(grantResults.isNotEmpty()&& grantResults[0] == PackageManager.PERMISSION_GRANTED){
                val intent = Intent(MediaStore.ACTION_IMAGE_CAPTURE) 
                startActivityForResult(intent, CAMERA_REQUEST_CODE)
            }else{
                Toast.makeText(this,"Permission is denied",Toast.LENGTH_SHORT).show()
            }
        }
    }

CodePudding user response:

Let me give you a very simple solution, try to use this library - https://github.com/dawnimpulse/permissions-android

CodePudding user response:

Requesting runtime permissions is just a little more simplified

private val requestPermission =
    registerForActivityResult(ActivityResultContracts.RequestPermission()) { isGranted ->
        // do something
    }

Now we can call this to get any type of permission you want

cameraButton.setOnClickListener {
        
        if (ContextCompat.checkSelfPermission(
                this,
                Manifest.permission.CAMERA
            ) != PackageManager.PERMISSION_GRANTED
        ) {
            // Pass any permission you want while launching
            requestPermission.launch(Manifest.permission.CAMERA)
        }
    }

Make sure to add in build.gradle

implementation 'androidx.fragment:fragment-ktx:1.2.0' // or later
implementation 'androidx.activity:activity-ktx:1.3.0' // or later

If you want to understand how all this works check here

  • Related