所以我有一个组合程序,它试图从存储中读取数据,
@Composable
private fun Screen() {
val launcher = rememberLauncherForActivityResult(contract = ActivityResultContracts.StartActivityForResult()) { result ->
val uri = result.data?.data.toString()
if(uri !== null) {
val file = File(uri)
val bytes = file.readBytes()
println(bytes)
}
}
Column() {
Button(onClick = {
val intent = Intent().setType("*/*").setAction(Intent.ACTION_OPEN_DOCUMENT)
launcher.launch(intent)
}) {
Text("Open file")
}
}
}
但是,它给了我一个错误:content:/com.android.providers.downloads.documents/document/raw%3A%2Fstorage%2Femulated%2F0%2FDownload%2FIMG_CEFEFF486A8C-1.jpeg: open failed: ENOENT (No such file or directory)
。我在这里做错了什么?请帮忙。
想清楚了,
@Composable
private fun Screen() {
val context = LocalContext.current
val launcher = rememberLauncherForActivityResult(contract = ActivityResultContracts.GetContent()) { result ->
val item = context.contentResolver.openInputStream(result)
val bytes = item?.readBytes()
println(bytes)
item?.close()
}
return Column {
Button(onClick = {
launcher.launch("*/*")
}) {
Text("Open file")
}
}
}