Search code examples
androidkotlinandroid-permissions

How to request a permission on Android


I'm new to using Android Studio, just want to make a basic app for myself, and I can't request any permission on my phone, it just refuses automatically. I'm using Kotlin but also tried in Java (as I studied Java years ago but never learned to code on Android). My phone is a OnePlus Nord 2t (if that makes any difference), and is using API 33. My project has a minimum SDK of API 30.

I already made sure the build.gradle minSDK and targetSdk are ok, and added this line to the AndroidManifest.xml :

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
// also tried to add tools:node="replace" and tools:remove="android:maxSdkVersion" because I saw people mentionning it

Here's the whole code I have for now

package com.example.testpermissions

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

class MainActivity : AppCompatActivity() {

    private val READ_EXTERNAL_STORAGE_PERMISSION_CODE = 101

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

        // Check if the permission is already granted
        if (ContextCompat.checkSelfPermission(
                this,
                Manifest.permission.READ_EXTERNAL_STORAGE
            ) == PackageManager.PERMISSION_GRANTED
        ) {
            // Permission is already granted.
            Toast.makeText(this, "already ok", Toast.LENGTH_SHORT).show()
        } else {
            Toast.makeText(this, "prompting", Toast.LENGTH_SHORT).show()
            // Permission is not granted.
            ActivityCompat.requestPermissions(
                this,
                arrayOf(Manifest.permission.READ_EXTERNAL_STORAGE),
                READ_EXTERNAL_STORAGE_PERMISSION_CODE
            )
        }
    }

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

        if (requestCode == READ_EXTERNAL_STORAGE_PERMISSION_CODE) {
            if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                // Permission granted.
                Toast.makeText(this, "ok", Toast.LENGTH_SHORT).show()
            } else {
                Toast.makeText(this, "not ok", Toast.LENGTH_SHORT).show()
                // Permission denied by the user.
            }
        }
    }
}

As you can see I used Toasts to see what's working or not, and all I have is "prompting" then "not ok", and the app continues running normally after that.

Thanks you in advance for your help.


Solution

  • If you go to the documentation, you will see that:

    Note: Starting in API level 33, this permission has no effect. If your app accesses other apps' media files, request one or more of these permissions instead: READ_MEDIA_IMAGES, READ_MEDIA_VIDEO, READ_MEDIA_AUDIO. Learn more about the storage permissions that are associated with media files.

    Therefore, if you require to access specific media files, you need to ask for a different permission.