简体   繁体   English

android 11中/storage/emulated/路径下的read.txt字段数据

[英]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.

相关问题 FileSystemException:无法打开文件,路径 = &#39;目录:&#39;/storage/emulated/0/Android/data/ - FileSystemException: Cannot open file, path = 'Directory: '/storage/emulated/0/Android/data/ 无法播放路径中的视频 (/storage/emulated/0/Android/data/com...) - Could not play video in the path (/storage/emulated/0/Android/data/com…) 无法在 android 的 /storage/emulated/0/Android/data/myproject.data/data 中创建目录 11 - Not able to create directory in /storage/emulated/0/Android/data/myproject.data/data in android 11 Android设备上的file:/// storage / emulated / 0路径 - file:///storage/emulated/0 path on android device 如何在Android 6上获得模拟的存储路径? - How to get emulated storage path on Android 6? Android:无法读取/存储/模拟文件夹 - Android: Not able to read /storage/emulated folder 迁移 /storage/emulated/0/ 中的文件夹<my_folder>从 Android 10 到 11</my_folder> - Migrate folder in /storage/emulated/0/<my_folder> from Android 10 to 11 Android / storage / emulated / 0和/ data / media / 0不同的权限 - Android /storage/emulated/0 and /data/media/0 different permissions 我不明白为什么在 Android 11 中使用 `storage\emulated...\downloads` 文件夹可以读取/写入某些文件而不是其他文件? - I don't understand why in Android 11 use of `storage\emulated...\downloads` folder can read/write some files and not others? 从 data.getData() 类型的 URI 获取真实路径 file:///storage/emulated/0/LenovoReaper/did - get real path from URI of type data.getData() file:///storage/emulated/0/LenovoReaper/did
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM