简体   繁体   中英

Jetpack Compose with Accompanist Permissions to access local files

I want to use a keystore file that I placed in libs directory. i added the permissions in the 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"/>

I requested the permission during runtime as well using a Accompanist Permissions this way:

@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)
    }
}

I asked for these permissions:

val PERMISSIONS_LIST = listOf(Manifest.permission.CAMERA, Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE)

on the app first run I get the permission request and I give it an access the first one is for using camera and recording videos, the second one for accessing photos and media contents.

but I'm still getting this same error again and again:

failed with exception: java.io.FileNotFoundException: app/libs/keystore/keypos.jks: open failed: ENOENT (No such file or directory)

How can fix this issue? I can't use the ssl settings because of this issue.

It seems that reading a local file from the app package can be done in another way.

I placed the keystore in the res directory, in its own directory. and used the context to get it.

val file = context?.resources?.openRawResource(R.raw.keypos)

This is it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM