Jetpack Compose 具有访问本地文件的伴奏权限
Jetpack Compose with Accompanist Permissions to access local files
我想使用我放在 libs 目录中的密钥库文件。
我在 AndroidManifest.xl
中添加了权限
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-sdk tools:overrideLibrary="com.github.dhaval2404.colorpicker,com.datacap.device_connection" />
<uses-permission android:name="com.google.android.things.permission.MANAGE_INPUT_DRIVERS"/>
我在 运行 期间也通过 Accompanist Permissions
以这种方式请求了许可:
@OptIn(ExperimentalPermissionsApi::class)
@Composable
fun PermissionsRequest(
permissions: List<String>,
deniedMessage: String,
rationaleMessage: String
) {
val multiplePermissionsState = rememberMultiplePermissionsState(permissions = permissions)
HandelRequest(
multiplePermissionsState = multiplePermissionsState,
deniedContent = { shouldShowRationale ->
PermissionDeniedContent(
deniedMessage = deniedMessage,
rationaleMessage = rationaleMessage,
shouldShowRationale = shouldShowRationale,
onRequestPermission = { multiplePermissionsState.launchMultiplePermissionRequest() }
)
},
content = {}
)
}
@Composable
fun Content(text: String, showButton: Boolean = true, onClick: () -> Unit) {
Column(
modifier = Modifier
.fillMaxSize()
.padding(50.dp)
.background(color = Color.Gray),
verticalArrangement = Arrangement.Center,
horizontalAlignment = Alignment.CenterHorizontally
) {
Text(text = text, textAlign = TextAlign.Center)
Spacer(modifier = Modifier.height(12.dp))
if (showButton) {
Button(onClick = onClick) {
Text(text = "Give Permission")
}
}
}
}
@OptIn(ExperimentalPermissionsApi::class)
@Composable
fun HandelRequest(
multiplePermissionsState: MultiplePermissionsState,
deniedContent: @Composable (Boolean) -> Unit,
content: @Composable () -> Unit
) {
var shouldShowRationale by remember { mutableStateOf(false) }
val result = multiplePermissionsState.permissions.all {
shouldShowRationale = it.status.shouldShowRationale
it.status == PermissionStatus.Granted
}
if (!result) deniedContent(shouldShowRationale)
}
@ExperimentalPermissionsApi
@Composable
fun PermissionDeniedContent(
deniedMessage: String,
rationaleMessage: String,
shouldShowRationale: Boolean,
onRequestPermission: () -> Unit
) {
if (shouldShowRationale) {
AlertDialog(
onDismissRequest = {},
title = {
Text(
text = "Permission Request",
style = TextStyle(
fontSize = MaterialTheme.typography.h6.fontSize,
fontWeight = FontWeight.Bold
)
)
},
text = {
Text(rationaleMessage)
},
confirmButton = {
Button(onClick = onRequestPermission) {
Text("Give Permission")
}
}
)
} else {
Content(text = deniedMessage, onClick = onRequestPermission)
}
}
我请求了这些权限:
val PERMISSIONS_LIST = listOf(Manifest.permission.CAMERA, Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE)
首先在应用程序上 运行 我收到权限请求并授予它访问权限,第一个用于使用相机和录制视频,第二个用于访问照片和媒体内容。
但我仍然一次又一次地遇到同样的错误:
failed with exception: java.io.FileNotFoundException: app/libs/keystore/keypos.jks: open failed: ENOENT (No such file or directory)
如何解决这个问题?由于这个问题,我无法使用 ssl 设置。
从应用包中读取本地文件好像可以换个方式。
我将密钥库放在 res
目录中,在它自己的目录中。并使用上下文来获取它。
val file = context?.resources?.openRawResource(R.raw.keypos)
就是这个。
我想使用我放在 libs 目录中的密钥库文件。
我在 AndroidManifest.xl
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-sdk tools:overrideLibrary="com.github.dhaval2404.colorpicker,com.datacap.device_connection" />
<uses-permission android:name="com.google.android.things.permission.MANAGE_INPUT_DRIVERS"/>
我在 运行 期间也通过 Accompanist Permissions
以这种方式请求了许可:
@OptIn(ExperimentalPermissionsApi::class)
@Composable
fun PermissionsRequest(
permissions: List<String>,
deniedMessage: String,
rationaleMessage: String
) {
val multiplePermissionsState = rememberMultiplePermissionsState(permissions = permissions)
HandelRequest(
multiplePermissionsState = multiplePermissionsState,
deniedContent = { shouldShowRationale ->
PermissionDeniedContent(
deniedMessage = deniedMessage,
rationaleMessage = rationaleMessage,
shouldShowRationale = shouldShowRationale,
onRequestPermission = { multiplePermissionsState.launchMultiplePermissionRequest() }
)
},
content = {}
)
}
@Composable
fun Content(text: String, showButton: Boolean = true, onClick: () -> Unit) {
Column(
modifier = Modifier
.fillMaxSize()
.padding(50.dp)
.background(color = Color.Gray),
verticalArrangement = Arrangement.Center,
horizontalAlignment = Alignment.CenterHorizontally
) {
Text(text = text, textAlign = TextAlign.Center)
Spacer(modifier = Modifier.height(12.dp))
if (showButton) {
Button(onClick = onClick) {
Text(text = "Give Permission")
}
}
}
}
@OptIn(ExperimentalPermissionsApi::class)
@Composable
fun HandelRequest(
multiplePermissionsState: MultiplePermissionsState,
deniedContent: @Composable (Boolean) -> Unit,
content: @Composable () -> Unit
) {
var shouldShowRationale by remember { mutableStateOf(false) }
val result = multiplePermissionsState.permissions.all {
shouldShowRationale = it.status.shouldShowRationale
it.status == PermissionStatus.Granted
}
if (!result) deniedContent(shouldShowRationale)
}
@ExperimentalPermissionsApi
@Composable
fun PermissionDeniedContent(
deniedMessage: String,
rationaleMessage: String,
shouldShowRationale: Boolean,
onRequestPermission: () -> Unit
) {
if (shouldShowRationale) {
AlertDialog(
onDismissRequest = {},
title = {
Text(
text = "Permission Request",
style = TextStyle(
fontSize = MaterialTheme.typography.h6.fontSize,
fontWeight = FontWeight.Bold
)
)
},
text = {
Text(rationaleMessage)
},
confirmButton = {
Button(onClick = onRequestPermission) {
Text("Give Permission")
}
}
)
} else {
Content(text = deniedMessage, onClick = onRequestPermission)
}
}
我请求了这些权限:
val PERMISSIONS_LIST = listOf(Manifest.permission.CAMERA, Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE)
首先在应用程序上 运行 我收到权限请求并授予它访问权限,第一个用于使用相机和录制视频,第二个用于访问照片和媒体内容。
但我仍然一次又一次地遇到同样的错误:
failed with exception: java.io.FileNotFoundException: app/libs/keystore/keypos.jks: open failed: ENOENT (No such file or directory)
如何解决这个问题?由于这个问题,我无法使用 ssl 设置。
从应用包中读取本地文件好像可以换个方式。
我将密钥库放在 res
目录中,在它自己的目录中。并使用上下文来获取它。
val file = context?.resources?.openRawResource(R.raw.keypos)
就是这个。