Search code examples
androidkotlinandroid-permissions

Read external storage Permission not working


I want to later in my app be able to choose an Image to be displayed in the PhotoView and it is supposed to stay there after restarting the app. For this, I need to read the External storage, but after trying everything on the Internet I can't get it to ask me for permission, it successfully checks that I have no such permission but does not ask me to grant it, I am fairly new to this so sorry if there is something stupid I overlooked:

if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE)
            != PackageManager.PERMISSION_GRANTED) {
            // Permission is not granted, request it
            ActivityCompat.requestPermissions(this,
                arrayOf(Manifest.permission.READ_EXTERNAL_STORAGE),
                PERMISSION_REQUEST_EXTERNAL_STORAGE)
            Toast.makeText(this, "Permission NOT granted", Toast.LENGTH_SHORT).show()
        } else {
            // Permission has been granted, you can access external storage here
            // Your code to read external storage goes here
            Toast.makeText(this, "Permission granted", Toast.LENGTH_SHORT).show()
        }

this is the Code to ask for permission, and it always returns: "Permission NOT granted", i have the following lines in the Manifest File:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">

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

and i have this Function:

        override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
            super.onRequestPermissionsResult(requestCode, permissions, grantResults)
            if (requestCode == PERMISSION_REQUEST_EXTERNAL_STORAGE) {
                if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    // Permission granted, you can access external storage now
                    // Your code to read external storage goes here
                } else {
                    // Permission denied
                    // Handle the case where the user denied the permission
                }
            }
        }

I have declared the:

private val PERMISSION_REQUEST_EXTERNAL_STORAGE = 1

Although to be honest, I do not fully understand why it is supposed to be one. Every time I launch the app, it just straight up says permission not granted and executes everything else normally, simply returning the error when the external storage is requested. Thanks in advance.


Solution

  • Seems like from your answer, your device is above Android 10 and for Android 10 and above, the permissions READ_EXTERNAL_STORAGE has been deprecated and in later versions WRITE_EXTERNAL_STORAGE as well

    1. You can use the PhotoPicker component available. You can read about the implementation details here

    2. From Android 11, you can access the directories specific to your app without any additional permissions. You can read about this.

    3. You can use the MANAGE_EXTERNAL_STORAGE permission, which can let you access any folders on the storage. But there's a catch here,this is available only to File Managers and special apps and won't go through the Play Store review. Read about this here