![](/img/trans.png)
[英]FileSystemException: Cannot open file, path = 'Directory: '/storage/emulated/0/Android/data/
[英]Read .txt field data from /storage/emulated/ path in android 11
我想从 /storage/emulated 应用程序特定路径的.txt
文件中读取数据。 我已在同一个文件中成功写入数据,但无法读取。
将数据写入txt文件的代码。
val writer: FileOutputStream = openFileOutput(file.absolutePath, MODE_PRIVATE)
writer.write(str1.toByteArray())
writer.flush()
writer.close()
试图从同一个文件中读取数据。
val text = StringBuilder()
val br = BufferedReader(FileReader(file))
var line: String?
while (br.readLine().also { line = it } != null) {
text.append(line)
text.append('\n')
}
br.close()
行返回 null 值。
Try adding android:requestLegacyExternalStorage="true" to your manifest.
或者更改它以符合 Android 11 的新范围存储系统。 请参阅https://developer.android.com/about/versions/11/privacy/storage
我想从 /storage/emulated 应用程序特定路径的 .txt 文件中读取数据。 我已在同一个文件中成功写入数据,但无法读取。
您没有写入/storage/emulated
目录中的某个位置。 您使用openFileOutput()
。 写入不同的位置。 要从该位置读取,请使用openFileInput()
,即Context
上的相应方法。 请务必使用您传递给openFileOutput()
的相同文件名。
您应该写入位置/storage/emulated
目录。 您已经使用过openFileOutput()
。 它指向 Context 上的getFilesDir()
返回的位置。 你可以这么说,因为文档说:
返回文件系统上存储使用 openFileOutput(String, int) 创建的文件的目录的绝对路径。
要从您的位置读取,请使用openFileInput()
,即 Context 上的相应方法,文件名与您传递给openFileOutput()
的文件名相同。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.