Ask for READ_EXTERNAL_STORAGE permsssion for API level 30

Since API level 30 the WRITE_EXTERNAL_STORAGE permission will be
ignored. Instead the READ_EXTERNAL_STORAGE permission must be used.

See: https://developer.android.com/training/data-storage

Signed-off-by: Tim Krüger <t@timkrueger.me>
This commit is contained in:
Tim Krüger 2021-09-14 13:54:20 +02:00
parent b5960f9b97
commit 5cacf7767d
No known key found for this signature in database
GPG Key ID: FECE3A7222C52A4E

View File

@ -213,31 +213,64 @@ class UploadAndShareFilesWorker(val context: Context, workerParameters: WorkerPa
const val META_DATA = "META_DATA"
fun isStoragePermissionGranted(context: Context): Boolean {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
return if (PermissionChecker.checkSelfPermission(
context,
Manifest.permission.WRITE_EXTERNAL_STORAGE
) == PermissionChecker.PERMISSION_GRANTED
) {
Log.d(TAG, "Permission is granted")
true
} else {
Log.d(TAG, "Permission is revoked")
false
when {
Build.VERSION.SDK_INT > 29 -> {
return if (PermissionChecker.checkSelfPermission(
context, Manifest.permission
.READ_EXTERNAL_STORAGE
) == PermissionChecker.PERMISSION_GRANTED
) {
Log.d(TAG, "Permission is granted (SDK 30 or greater)")
true
} else {
Log.d(TAG, "Permission is revoked (SDK 30 or greater)")
false
}
}
Build.VERSION.SDK_INT >= Build.VERSION_CODES.M -> {
return if (PermissionChecker.checkSelfPermission(
context,
Manifest.permission.WRITE_EXTERNAL_STORAGE
) == PermissionChecker.PERMISSION_GRANTED
) {
Log.d(TAG, "Permission is granted")
true
} else {
Log.d(TAG, "Permission is revoked")
false
}
}
else -> { // permission is automatically granted on sdk<23 upon installation
Log.d(TAG, "Permission is granted")
return true
}
} else { // permission is automatically granted on sdk<23 upon installation
Log.d(TAG, "Permission is granted")
return true
}
}
fun requestStoragePermission(controller: Controller) {
controller.requestPermissions(
arrayOf(
Manifest.permission.WRITE_EXTERNAL_STORAGE
),
REQUEST_PERMISSION
)
when {
Build.VERSION.SDK_INT > 29 -> {
controller.requestPermissions(
arrayOf(
Manifest.permission.READ_EXTERNAL_STORAGE
),
REQUEST_PERMISSION
)
}
Build.VERSION.SDK_INT >= Build.VERSION_CODES.M -> {
controller.requestPermissions(
arrayOf(
Manifest.permission.WRITE_EXTERNAL_STORAGE
),
REQUEST_PERMISSION
)
}
else -> { // permission is automatically granted on sdk<23 upon installation
}
}
}
}
}