// 1
if
(isExternalStorageWritable()) {
// 2
FileOutputStream(noteFile(note.fileName)).use { output ->
output.write(note.noteText.toByteArray())
}
}
Here’s how it works:
1. Check to see if the external storage is available.
2. Open a
FileOutputStream
with
use
.
3. Write
note.noteText
to the file.
Next, in
MainActivity.kt
, change
the instance of the
NoteRepository
you’re
initializing to the following:
private
val repo:
NoteRepository
by
lazy
{
ExternalFileRepository
(this) }
Finally, run the program to write a file to the external storage. To
view the file using
the
Device File Explorer, look in
sdcard/data
, within the app’s package name folder.
You’ll implement the app’s read function in the next step.
Do'stlaringiz bilan baham: