Using Platform Specific Paths
Getting a path to a file is different for each platform, and you will need to define how this works for each platform
var storageDir: String
On Android
Getting a path on android involves invoking from filesDir
/cacheDir
from a Context
.
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// for documents directory
storageDir = filesDir.path
// or caches directory
storageDir = cacheDir.path
}
}
On Desktop (JVM)
This depends on where you want to save your files, but generally you should save your files in a user data directory. Recommending to use harawata's appdirs to get the platform specific app dir
storageDir = AppDirsFactory.getInstance()
.getUserDataDir(PACKAGE_NAME, VERSION, ORGANISATION)
On iOS & other Apple platforms
This depends on where you want to place your files. For most common use-cases, you will want either NSDocumentDirectory
or NSCachesDirectory
KStore provides you a convenience extensions to resolve these for you
// for documents directory
storageDir = NSFileManager.defaultManager.DocumentDirectory?.relativePath
// or caches directory
storageDir = NSFileManager.defaultManager.CachesDirectory?.relativePath
Last modified: 14 August 2024