Runtime Permission

This is important and most common now in android development, because every feature which is critical to use for user, you must ask user to allow access for it. Like camera, microphone, storage, location for every feature you must ask user at runtime, not at installation time. I have used for storage and I am sharing how I have requested permission for selecting picture from gallery.

To request permission:

        if(ContextCompat.checkSelfPermission(this, android.Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED){
        } else {
            ActivityCompat.requestPermissions(this, arrayOf(android.Manifest.permission.READ_EXTERNAL_STORAGE), PICK_IMAGE)

This is callback when user allow or denies permission.

	override fun onRequestPermissionsResult(
        requestCode: Int,
        permissions: Array,
        grantResults: IntArray
    ) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        if(requestCode == PICK_IMAGE && grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED){

Share this content: