0

在我的应用程序中,我必须请求许可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")
            }
        }
    )
}
4

0 回答 0