Answer the question
In order to leave comments, you need to log in
Why is the file not saved in External Storage?
I study data storage in Android according to the book Saving Data on Android by raywenderlich.
Their example uses the methods of such a repository.
class ExternalFileRepository(var context: Context) :
NoteRepository {
override fun addNote(note: Note) {
if (isExternalStorageWritable()) {
FileOutputStream(noteFile(note.fileName)).use { output ->
output.write(note.noteText.toByteArray())
}
}
}
override fun getNote(fileName: String): Note {
val note = Note(fileName, "")
if (isExternalStorageReadable()) {
FileInputStream(noteFile(fileName)).use { stream ->
val text = stream.bufferedReader().use {
it.readText()
}
note.noteText = text
}
}
return note
}
override fun deleteNote(fileName: String): Boolean {
return isExternalStorageWritable() && noteFile(fileName).delete()
}
private fun noteDirectory(): File? = context.getExternalFilesDir(null)
private fun noteFile(fileName: String): File = File(noteDirectory(), fileName)
fun isExternalStorageWritable(): Boolean {
return Environment.getExternalStorageState() == Environment.MEDIA_MOUNTED
}
fun isExternalStorageReadable(): Boolean {
return Environment.getExternalStorageState() in
setOf(Environment.MEDIA_MOUNTED, Environment.MEDIA_MOUNTED_READ_ONLY)
}
}
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question