Kotlin
val folderPath = getFilesDir(); // folderPath = "/data/user/0/com.example.androidkotlinapp1/files"
val filename = "1.txt"
var text = "Hello"
val data:ByteArray = text.toByteArray(Charset.forName("US-ASCII"))
val file = File(folderPath, filename); // create an object, but the file will not be created now
try {
val os:OutputStream = FileOutputStream(file);
os.write(data); // The file will be created and the data will be saved
os.close();
}
catch (e: IOException)
{
}
Пытаюсь использовать Ваш пример, но у меня getFilesDir() - unresolved :( и ничто не помогает её как-то объявить
Нет понимания - почему?
Решение:
val path = context.getFilesDir()
К информации:
Android метод getFilesDir объявлен в классе Context
В моем примере getFilesDir вызывается внутри класса AppCompatActivity
Класс AppCompatActivity (в документации Android) ... наследуется от класса Context answer