Home > Mobile >  Android kotlin: Getting EACCES (Permission denied) even after giving permissions
Android kotlin: Getting EACCES (Permission denied) even after giving permissions

Time:11-23

I am developing an android app and having trouble with the permission handling (API lvl 30)

I tried to follow the steps here: https://developer.android.com/training/permissions/requesting.html

When using the "checkSelfPermission()" function, it looks like I have the necessary permissions, but when trying to Write to a file I get the error mentioned above.

This is my code:

class FilesActivity : AppCompatActivity() {



val requestPermissionLauncher =
    registerForActivityResult(ActivityResultContracts.RequestPermission()) { isGranted: Boolean ->
        if (isGranted) {
            Log.d("access granted", isGranted.toString())

        } else {
            Log.d("access granted", isGranted.toString())
        }
    }


private lateinit var binding: ActivityFilesBinding

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)




    requestPermissions()

The method which checks the permissions looks like this:

    private fun requestPermissions() {
    when {
        ContextCompat.checkSelfPermission(
            this,
            Manifest.permission.WRITE_EXTERNAL_STORAGE
        ) == PackageManager.PERMISSION_GRANTED -> {
            Log.d("Permission","Already granted")
        }
        else -> {
            requestPermissionLauncher.launch(
                Manifest.permission.WRITE_EXTERNAL_STORAGE
            )
        }
    }
}

I get the Permissions-prompt and after granting permission I try to download a file. The code for downloading the file is in an adapter-class for a recyclerview.

                    try{
                    if(ContextCompat.checkSelfPermission(
                        mContext,
                        Manifest.permission.WRITE_EXTERNAL_STORAGE
                    ) == PackageManager.PERMISSION_GRANTED) {
                        val myFile = File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS),file.getFilename()!!)
                        val fileRef = Firebase.storage.getReferenceFromUrl(file.getUrl()!!)

                        fileRef.getFile(myFile).addOnSuccessListener {
                            // Local temp file has been created
                            Toast.makeText(mContext,"File downloaded to Downloads Dir.",Toast.LENGTH_LONG).show()
                        }.addOnFailureListener {
                            Toast.makeText(mContext,it.toString(),Toast.LENGTH_LONG).show()
                        }
                    }

                } catch(e :Error){
                    Toast.makeText(mContext,e.toString(),Toast.LENGTH_LONG).show()
                }

I am not sure, what I did wrong. Maybe someone here can help me. Thanks in advance

CodePudding user response:

You haven't shown us your AndroidManifest.xml file, so it might be worth checking if you have added the necessary permissions in this file.

CodePudding user response:

Extends ActivityCompat.OnRequestPermissionsResultCallback is not available in FilesActivity class.

class FilesActivity : AppCompatActivity() , ActivityCompat.OnRequestPermissionsResultCallback {
}

You need to call the ActivityCompat.OnRequestPermissionsResultCallback interface for the requestPermissionLauncher definition.

Sample Code :

class MainActivity : AppCompatActivity(), ActivityCompat.OnRequestPermissionsResultCallback {


private val requestPermissionLauncher = registerForActivityResult(
        ActivityResultContracts.RequestPermission()
    ) { isGranted: Boolean ->
        if (isGranted) {
            // Permission has been granted. Start camera preview Activity.

                startCamera()

        } else {
            // Permission request was denied.
            requestPermissions()
        }
    }



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

    requestPermissions()


}

private fun requestPermissions() {
    if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA)
        == PackageManager.PERMISSION_GRANTED) {
        // Permission has been granted. Start camera preview Activity.
        startCamera()

    } else  requestPermissionLauncher.launch(Manifest.permission.CAMERA)
}


private fun startCamera() {
    val intent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
    startActivity(intent)
}

}

https://github.com/harunkor/requestPermissionLauncherSample

  • Related