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.
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:
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.