[英]Read .txt field data from /storage/emulated/ path in android 11
I want to read data from .txt
file from /storage/emulated application-specific path.我想从 /storage/emulated 应用程序特定路径的.txt
文件中读取数据。 I have written data successfully in same file but not able to read it.我已在同一个文件中成功写入数据,但无法读取。
Code to write data in txt file.将数据写入txt文件的代码。
val writer: FileOutputStream = openFileOutput(file.absolutePath, MODE_PRIVATE)
writer.write(str1.toByteArray())
writer.flush()
writer.close()
Trying to read data from same file.试图从同一个文件中读取数据。
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()
line returning null value.行返回 null 值。
Try adding android:requestLegacyExternalStorage="true" to your manifest.
Or change it to comply to Android 11's new scoped storage system.或者更改它以符合 Android 11 的新范围存储系统。 Seehttps://developer.android.com/about/versions/11/privacy/storage请参阅https://developer.android.com/about/versions/11/privacy/storage
I want to read data from.txt file from /storage/emulated application-specific path.我想从 /storage/emulated 应用程序特定路径的 .txt 文件中读取数据。 I have written data successfully in same file but not able to read it.我已在同一个文件中成功写入数据,但无法读取。
You did not write to a location in a /storage/emulated
directory.您没有写入/storage/emulated
目录中的某个位置。 You used openFileOutput()
.您使用openFileOutput()
。 That writes to a different location.写入不同的位置。 To read from that location, use openFileInput()
, the corresponding method on Context
.要从该位置读取,请使用openFileInput()
,即Context
上的相应方法。 Be sure to use the same filename that you passed to openFileOutput()
.请务必使用您传递给openFileOutput()
的相同文件名。
You should write to the location /storage/emulated
directory.您应该写入位置/storage/emulated
目录。 You avhave used openFileOutput()
.您已经使用过openFileOutput()
。 It points to where getFilesDir()
on Context returns.它指向 Context 上的getFilesDir()
返回的位置。 You can tell this because the documentation says:你可以这么说,因为文档说:
Returns the absolute path to the directory on the filesystem where files created with openFileOutput(String, int) are stored.返回文件系统上存储使用 openFileOutput(String, int) 创建的文件的目录的绝对路径。
To read from your location, use openFileInput()
, the corresponding method on Context with the same filename that you had passsed to the openFileOutput()
.要从您的位置读取,请使用openFileInput()
,即 Context 上的相应方法,文件名与您传递给openFileOutput()
的文件名相同。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.