Home > Mobile >  Why I can't ask for permissions in Kotlin?
Why I can't ask for permissions in Kotlin?

Time:02-19

I have this line, I want to ask for some permissions

ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE), 1)

Everything is fine, I changed the manifest, but the "permission" after the Manifest.permission is red, and showing as an error.

I'm following an Udemy course, the guy did it exactly like me, and he has no problem.

By the way, I'm writing that code inside a function, which is outside the onCreate, but inside the MainClass, so I don't get what I'm doing wrong

CodePudding user response:

You need to add import android.Manifest to your list of imports, so that the compiler knows where the right Manifest comes from. My guess is that you have something else named Manifest in your import list.

So, for example, in this class from this sample project, I have that import statement. I can then use Manifest.permission, such as:

requestPerm.launch(Manifest.permission.WRITE_EXTERNAL_STORAGE)

(FWIW, that sample is covered in this section of this free book)

  • Related