TheJadav

android runtime permissions

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

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){
            openPickerNow()
        } 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){
            openPickerNow()
        }
    }

Share this content:

Share:

More Posts

How does data persistence work in Android?

Data persistence in Android is the ability to save data to the device so that it can be accessed later, even if the app is closed or the device is restarted. There are several ways to achieve data persistence in Android

Fragments and Activities: Two Pillars of Android Development

Fragments and Activities are both important components of Android development. Fragments are a good choice for creating reusable and dynamic UIs, while Activities are a good choice for managing the lifecycle of an app’s UI and implementing core features.

Table of Contents

Send Us A Message