Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/227.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android java.io.FileNotFoundException:/storage/emulated/0/Download/myFile.pdf:打开失败:EACCES(权限被拒绝)_Android_Kotlin_Filenotfoundexception_Android Contentresolver - Fatal编程技术网

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

Android java.io.FileNotFoundException:/storage/emulated/0/Download/myFile.pdf:打开失败:EACCES(权限被拒绝),android,kotlin,filenotfoundexception,android-contentresolver,Android,Kotlin,Filenotfoundexception,Android Contentresolver,自从安卓Q问世以来,关于从外部存储读取的权限有一些隐私变化。我有一个聊天应用程序,用户可以选择从下载等照片。。。然后把它寄出去。所以我需要访问这些文件。我这样做的方式是使用contentProvider context.contentResolver.openInputStream(uri)?.use { inputStream -> while (true) { numBytesRead = inputStream.read(buffer) // ..

自从安卓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,但该文件确实存在

我已经在清单中设置了所有权限,并在应用程序启动时授予了这些权限。来自Android 我有一个聊天应用程序,用户可以选择从下载等照片。。。然后把它寄出去

这在Android 10及更高版本上是不可能的。你需要换一种方式。例如,您可以使用
ACTION\u OPEN\u DOCUMENT
允许用户从用户想要的任何位置选择内容。然后,使用生成的
Uri
将其上传到聊天服务器,类似于在代码片段中使用
Uri
。或者,更好的是,不要把它全部读入内存——使用类似的方法


对于Android 10,您可以向清单中的
元素添加
Android:requestLegacyExternalStorage=“true”
。这将使您选择传统存储模式,您现有的外部存储代码将正常工作。不过,这在Android R和更高版本上不起作用,因此这只是一个短期修复。

我觉得问题在于我有一个文件://uri,而没有一个内容://uri…@james04:这肯定没有帮助。你的问题并没有说明你是如何得到这个
Uri
的。我不必做我从哪里得到它的。关键是contentResolver没有理由抛出权限异常,因为它的工作是从外部源获取文件…当我找到error@james04:“我不必做我从哪里得到的”--是的,是的。你需要停止使用你所做的任何事情,因为在Android 10或更高版本上,
Uri
对你来说是无用的。您需要切换到使用
ACTION\u OPEN\u DOCUMENT
(或者可能是较旧的
ACTION\u GET\u CONTENT
)。如果您在三星S9设备上尝试此功能,您会发现它不起作用。相反,您的意图应该类似于intent=intent(“com.sec.android.app.myfiles.PICK_DATA”)