gpt4 book ai didi

android - java.io.FileNotFoundException:/storage/emulated/0/Download/myFile.pdf:打开失败:EACCES(权限被拒绝)

转载 作者:行者123 更新时间:2023-12-02 13:32:37 26 4
gpt4 key购买 nike

自从Android Q问世以来,从外部存储读取的权限在隐私方面有所变化。我有一个聊天应用程序,用户可以从下载等中选择照片并发送。因此,我需要访问该文件。我这样做的方法是使用contentProvider。

context.contentResolver.openInputStream(uri)?.use { inputStream ->
while (true) {
numBytesRead = inputStream.read(buffer)
// .. do stuff
}
}



当时可用的uri是-> file:///storage/emulated/0/Download/myFile.pdf

但是我得到FileNotFoundException但文件确实存在。

我已经在 list 中设置了所有权限,并在启动应用程序时授予了它们。从Android <= 9可以正常运行。
那我该怎么办...?

最佳答案

I have a chat application that the user can choose a photo from Downloads etc... and send it



在Android 10及更高版本上将无法实现。您需要切换到其他位置。例如,您可以使用 ACTION_OPEN_DOCUMENT允许用户从用户想要的任何地方选择内容。然后,使用生成的 Uri将其上传到您的聊天服务器,类似于您在代码段中使用 Uri的方式。或者,更好的是,不要将所有内容都读到内存中,请使用 this OkHttp InputStreamRequestBody implementation之类的东西。

对于Android 10,您可以在 list 中的 android:requestLegacyExternalStorage="true"元素中添加 <application>。这使您可以选择旧式存储模型,并且现有的外部存储代码将起作用。不过,这不适用于Android R及更高版本,因此这只是短期修复。

关于android - java.io.FileNotFoundException:/storage/emulated/0/Download/myFile.pdf:打开失败:EACCES(权限被拒绝),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60452085/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com