In my app I would like to generate backup files of database and shared preferences,this is the code to ask user permission to access external folders but dialog never appears
private fun checkStoragePermissions() {
if (ContextCompat.checkSelfPermission(
this, android.Manifest.permission.READ_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED || ContextCompat.checkSelfPermission(this,
android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED ) {
if(!ActivityCompat.shouldShowRequestPermissionRationale(this,
android.Manifest.permission.READ_EXTERNAL_STORAGE) ||ActivityCompat.shouldShowRequestPermissionRationale(this,
android.Manifest.permission.WRITE_EXTERNAL_STORAGE) ){
requestPermissions(
arrayOf<String>(
android.Manifest.permission.READ_EXTERNAL_STORAGE,
android.Manifest.permission.WRITE_EXTERNAL_STORAGE
), ALL_PERMISSION
)
}
}
else{
if (executeBackup)
backupFromDBData(WeakReference(this)).execute()
else restore_data()
}
}
override fun onRequestPermissionsResult(
requestCode: Int,
permissions: Array<String>,
grantResults: IntArray
) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
when (requestCode) {
ALL_PERMISSION -> if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
if (executeBackup)
backupFromDBData(WeakReference(this)).execute()
else restore_data()
return
}
}
}
Manifest
`<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
<uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE"
tools:ignore="ScopedStorage" />
`
checkStoragePermissions It is called by clicking on a Button
I tried different libraries, even the one I use when starting the MainActivity asks for permission for Notifications but in this case nothing is ever requested