I am trying to understand how Android internal storage works. For that I read a few tutorials and a number of posts on StackOverFlow.
Nevertheless things are not all that clear when testing real code.
Here is one problem, the following code is meant to create a directory and a file:
val dirName = "myNewDir"
createDir(dirName)
val fileName = "myNewFile"
writeToFile("Some random text for testing purpose ...",fileName)
val directory = filesDir
val files: Array<File> = directory.listFiles()
println("Files count = "+files.size)
for (f in files) {
println("Name: "+f.name)
}
This is the code for the two functions createDir() and writeToFile() :
fun createDir(dirName:String): File? {
return applicationContext.getDir(dirName, MODE_PRIVATE)
} /* End of createDir */
fun writeToFile(dataBufr:String, fileName:String) {
applicationContext.openFileOutput(fileName, Context.MODE_PRIVATE).use {
output -> output.write(dataBufr.toByteArray())
}
} /* End of writeToFile */
And this is what appears in the console when executing the code above:
I/System.out: Files count = 1
I/System.out: Name: myNewFile
My question is: Why is the directory (myNewDir) not created or at list does not appear in the console?
Latest android version, all default dirs will be created in the first open by LoadAPK.java,
getDirwill get the exists dir.In the
getDataDir()In the
frameworks/base/core/java/android/app/LoadedApk.javaall file will be init by ApplicationInfo.frameworks/base/core/java/android/os/FileUtils.java