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

  • To get an image from photo picker, you no longer need permissions. The following should just work.

    // Registers a photo picker activity launcher in single-select mode.
    val pickMedia = registerForActivityResult(PickVisualMedia()) { uri ->
        // Callback is invoked after the user selects a media item or closes the
        // photo picker.
        if (uri != null) {
            Log.d("PhotoPicker", "Selected URI: $uri")
        } else {
            Log.d("PhotoPicker", "No media selected")
        }
    }
    
    // Include only one of the following calls to launch(), depending on the types
    // of media that you want to let the user choose from.
    
    // Launch the photo picker and let the user choose images and videos.
    pickMedia.launch(PickVisualMediaRequest(PickVisualMedia.ImageAndVideo))
    
    // Launch the photo picker and let the user choose only images.
    pickMedia.launch(PickVisualMediaRequest(PickVisualMedia.ImageOnly))
    
    // Launch the photo picker and let the user choose only videos.
    pickMedia.launch(PickVisualMediaRequest(PickVisualMedia.VideoOnly))
    
    // Launch the photo picker and let the user choose only images/videos of a
    // specific MIME type, such as GIFs.
    val mimeType = "image/gif"
    pickMedia.launch(PickVisualMediaRequest(PickVisualMedia.SingleMimeType(mimeType)))
    

    Referred this