Supported on Android, iOS, macOS, JVM, Kotlin/Native Linux, JS and WASM targets
Introduction
PlatformFile is the core class in FileKit that provides a unified representation of files across all platforms. It abstracts away platform-specific file implementations and provides a consistent API for working with files in your Kotlin Multiplatform project.
Creating a PlatformFile
You can create aPlatformFile instance in several ways:
Platform-specific constructors
Each platform also provides specific constructors:Properties
PlatformFile provides several properties to access file information:
name is resolved using provider metadata. For Photo Picker URIs, FileKit performs a best-effort lookup of MediaStore.MediaColumns.DISPLAY_NAME when providers return synthetic names (for example 18.jpg), then falls back to a stable provider/URI-derived name if the original filename is not accessible.
For Android content URIs, parent() supports external-storage document trees and preserves access to the selected tree. It returns null at the selected directory’s root and for other content providers whose parent cannot be determined from the URI. Filesystem paths continue to use normal filesystem parents.
On JS/WASM, directory picker results are virtual directory trees built from browser file entries. Empty directories are not exposed by browsers, and directory lastModified() values are synthetic.
File operations
PlatformFile provides methods for common file operations:
Determining MIME types
UsemimeType() to ask the underlying platform for the best-known media type. It returns null when the value is unknown (for example, directories or proprietary containers).
- Apple platforms: FileKit first queries provider metadata (e.g., from the Files app or iCloud Drive) and falls back to the filename extension when needed, so even extension-less documents can resolve to a MIME type.
- Android: The resolver consults the
ContentResolverforcontent://URIs and then falls back toMimeTypeMapif necessary. - JVM Desktop & Web: The lookup is derived from the file extension or the desktop platform’s MIME registry.
- Kotlin/Native Linux: FileKit matches the filename against the system shared MIME database, with
/etc/mime.typesas a fallback.
Working with directories
You can use theresolve method or the / operator to navigate through directories:
Walking a directory tree
walk() lazily yields every file and subdirectory below a directory, depth-first, without the directory itself. sizeRecursively() returns the total size of the regular files in the tree:
- Symbolic links are listed but never followed, so a link can’t cause an infinite loop. They add nothing to
sizeRecursively(). This also applies when you call either function on a link. - On a regular file,
walk()yields only that file andsizeRecursively()returns its size.maxDepthmust be positive. walk()is synchronous, likelist(): on non-web platforms, iterate it onDispatchers.IO.sizeRecursively()is a suspend function that switches toDispatchers.IOitself on non-web platforms.- Android: each subdirectory of a document tree costs provider queries, so large trees are slow. Files whose provider doesn’t report a size count as 0.
- Apple platforms: keep security-scoped access to the root active while iterating
walk(), for example insidewithScopedAccess { }. - Windows: every reparse point counts as a link, including directory junctions and OneDrive Files On-Demand folders.
walk()lists such a folder without its contents, andsizeRecursively()doesn’t count them. - Web: works on directories picked with the directory picker, whose tree is already in memory. Both functions run on the caller’s dispatcher. Origin private file system directories aren’t supported.
Converting to kotlinx-io
PlatformFile provides built-in integration with kotlinx-io.
Example usage
Next steps
Read File
Read files as bytes, strings, or streams across platforms.
Write File
Write data to files with atomic operations and appending.
Image & Video Utils
Compress, resize and save images with platform-specific optimizations.
File Utils
Access standard directories and common file operations.
Bookmark Data
Maintain persistent access to user-selected files across app restarts.