在我的应用程序中,我必须请求write to external storage
的许可,以便保存位图以便能够共享它。现在看看下面的代码,每当我调用它,警报对话框出现,但no permission request appears
来自Android本身。所有我能在日志中看到的是Let's launch permission request.
如何适当地显示权限窗口从Android获得该权限?谢谢你。
@ExperimentalPermissionsApi
@Composable
fun Permission(
permission: String = android.Manifest.permission.WRITE_EXTERNAL_STORAGE,
rationale: String = "This permission is important for this app. Please grant the permission.",
permissionNotAvailableContent: @Composable () -> Unit = { },
content: @Composable () -> Unit = { }
) {
val permissionState = rememberPermissionState(permission)
PermissionRequired(
permissionState = permissionState,
permissionNotGrantedContent = {
Rationale(
text = rationale,
onRequestPermission = {
println("Let's launch permission request.")
permissionState.launchPermissionRequest()
}
)
},
permissionNotAvailableContent = permissionNotAvailableContent,
content = content
)
}
@Composable
private fun Rationale(
text: String,
onRequestPermission: () -> Unit
) {
AlertDialog(
onDismissRequest = { /* Don't */ },
title = {
Text(text = "Permission request")
},
text = {
Text(text)
},
confirmButton = {
Button(onClick = onRequestPermission) {
Text("Ok")
}
}
)
}
只是为了没有人错过ADM在评论中提到的可能的解决方案。
The Solution by ADM:检查您是否在清单中添加了权限