free geoip
53

Fix Kotlin Permission Denied Error Easily

How to Solve Permission Denied Error in Kotlin When developing Android apps with Kotlin, one of the most common issues…

How to Solve Permission Denied Error in Kotlin

When developing Android apps with Kotlin, one of the most common issues developers encounter is the “Permission Denied” error. This usually occurs when the app tries to access protected resources like the camera, location, or external storage without the appropriate runtime permissions.

Permission Denied Kotlin

This guide will help you fix this issue step-by-step, using modern permission handling in Kotlin with complete code examples.

1. Understand the Cause

The error usually stems from one of two reasons:

  • You didn’t declare the permission in the AndroidManifest.xml
  • You didn’t request the permission at runtime (for dangerous permissions, Android 6.0+)

2. Add Permission to Manifest

Make sure you declare the required permissions. For example, for accessing external storage:

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

3. Request Permission in Kotlin Code

You can request runtime permissions using ActivityCompat. Below is a simple example of a permission request for external storage.

MainActivity.kt

package com.example.permissionfix

import android.Manifest
import android.content.pm.PackageManager
import android.os.Bundle
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat

class MainActivity : AppCompatActivity() {

    private val STORAGE_PERMISSION_CODE = 1001

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        checkPermission()
    }

    private fun checkPermission() {
        if (ContextCompat.checkSelfPermission(
                this,
                Manifest.permission.READ_EXTERNAL_STORAGE
            ) == PackageManager.PERMISSION_GRANTED
        ) {
            showToast("Permission already granted")
        } else {
            requestPermission()
        }
    }

    private fun requestPermission() {
        ActivityCompat.requestPermissions(
            this,
            arrayOf(Manifest.permission.READ_EXTERNAL_STORAGE),
            STORAGE_PERMISSION_CODE
        )
    }

    override fun onRequestPermissionsResult(
        requestCode: Int,
        permissions: Array<out String>,
        grantResults: IntArray
    ) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)

        if (requestCode == STORAGE_PERMISSION_CODE) {
            if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                showToast("Permission granted")
            } else {
                showToast("Permission denied")
            }
        }
    }

    private fun showToast(message: String) {
        Toast.makeText(this, message, Toast.LENGTH_SHORT).show()
    }
}

4. Use Permission Libraries (Optional)

You can simplify permission requests using libraries like Dexter if you want better UX and cleaner code

5. Handle Denied Permissions

Always guide users when they deny permissions. If they deny and choose “Don’t ask again”, guide them to the app settings.

val intent = Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS)
val uri: Uri = Uri.fromParts("package", packageName, null)
intent.data = uri
startActivity(intent)

Conclusion

To avoid Permission Denied errors in Kotlin apps, make sure to:

  • Declare permissions in the manifest
  • Request runtime permissions properly
  • Handle user responses (granted/denied)

Following the above methods will help you avoid permission issues and improve your app’s reliability.

rysasahrial

Leave a Reply

Your email address will not be published. Required fields are marked *