Been Googling, followed the documentation. The dialogue doesn't show up at all. I put the uses-permission in the manifest. I don't understand what I am doing wrong. I get to the else branch where it is supposed to launch the permission, but nothing shows up. This seems like such a simple thing but I can't get it to work. Any help would be great.
This is my code from my main activity:
private val requestPermissionLauncher =
registerForActivityResult(
ActivityResultContracts.RequestPermission()
) { isGranted: Boolean ->
if (isGranted) {
Log.d(TAG, "Permission granted.")
} else {
Log.d(TAG, "Permission denied.")
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
when (PackageManager.PERMISSION_GRANTED) {
ContextCompat.checkSelfPermission(
applicationContext,
Manifest.permission.READ_EXTERNAL_STORAGE
) -> {
Log.d(TAG, "Permission denied.")
// You can use the API that requires the permission.
}
else -> {
Log.d(TAG, "Permission denied, launcher launched.")
// You can directly ask for the permission.
// The registered ActivityResultCallback gets the result of this request.
requestPermissionLauncher.launch(
Manifest.permission.READ_EXTERNAL_STORAGE
)
}
}
}
this permission is depreciated in hign version of Android.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
requestList.add(Manifest.permission.READ_MEDIA_IMAGES)
requestList.add(Manifest.permission.READ_MEDIA_AUDIO)
requestList.add(Manifest.permission.READ_MEDIA_VIDEO)
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
requestList.add(Manifest.permission.READ_EXTERNAL_STORAGE)
} else {
requestList.add(Manifest.permission.READ_EXTERNAL_STORAGE)
requestList.add(Manifest.permission.WRITE_EXTERNAL_STORAGE)
}
please use this code to build the permissions you need to access external storage.