我正在尝试从外部存储中读取文件/some/path/somefile.txt
在清单中我有<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
当我单击按钮尝试读取ActivityResultContracts.OpenDocument()
我选择的文件时
java.io.FileNotFoundException: /document/primary:some/path/somefile.txt: open failed: ENOENT (No such file or directory)
这是我的代码:
@OptIn(ExperimentalPermissionsApi::class)
@Composable
fun ReadFileScreen() {
val readPermissionState = rememberPermissionState(
android.Manifest.permission.READ_EXTERNAL_STORAGE
)
val pickedFileUriState = remember { mutableStateOf<Uri?>(null) }
val launcher =
rememberLauncherForActivityResult(contract = ActivityResultContracts.OpenDocument()) { result ->
pickedFileUriState.value = result
}
Column {
Button(onClick = readPermissionState::launchPermissionRequest) {
Text("request read permission")
}
PermissionRequired(readPermissionState, {}, {}) {
Button(onClick = { launcher.launch(arrayOf("*/*")) }
) {
Text("pick file")
}
if (pickedFileUriState.value != null) Button(onClick = { readTextFile(pickedFileUriState.value!!) }
) {
Text("read file")
}
}
}
}
fun readTextFile(uri: Uri) {
try {
val text = File(uri.path).readText()
println(text)
} catch (e: Exception) {
e.printStackTrace()
}
}