PlatformFile is the file or directory type that every FileKit API takes and returns. It wraps the native representation of each platform (java.io.File, Android Uri, NSURL, a browser File) behind one set of properties and functions.
Create a PlatformFile
On every platform except the web, create a file from a path, or from a parent directory and a child path:nonWebMain
PlatformFile(path) also parses content:// and file:// strings into a Uri.
Properties
Instant is kotlin.time.Instant. To read the content, see Reading files. To list, copy, move, or delete, see File operations.
Check the MIME type
Serialization
PlatformFile is @Serializable through PlatformFileSerializer, so you can store it in your own serializable classes with the kotlinx.serialization plugin:
nonWebMain
path string and decoded with PlatformFile(path).
Serialization keeps the path, not the access rights. A file picked outside your app (an Android
content:// file, a security-scoped file on Apple platforms) may not be readable after a restart: use bookmark data for persistent access. On the web, PlatformFileSerializer throws at runtime.Platform notes
Android: content:// files
Android: content:// files
Files from pickers are usually
content:// Uris, not filesystem paths. name comes from the provider’s display name. For Photo Picker Uris that only expose a synthetic name (such as 18.jpg), FileKit looks up the original name in MediaStore, and falls back to a stable name derived from the provider and the Uri.parent() works inside external-storage document trees and keeps access to the selected tree. It returns null at the root of the selected directory and for providers whose parent can’t be derived from the Uri.iOS and macOS: security-scoped files
iOS and macOS: security-scoped files
Files picked outside your app’s sandbox need security-scoped access. FileKit starts and stops it around each of its own calls on that file. Wrap longer work in
file.withScopedAccess { }: iterating walk() over a picked folder, or passing nsUrl to Apple APIs yourself.Web: browser handles
Web: browser handles
A picked file is a read-only snapshot of a browser
File: you can read it, not write it, and there is no path to reopen it later. A directory picker result is a virtual tree built from the selected files, so empty folders are missing, directories report a size of 0, and their lastModified() is epoch 0. See Files on the web.Related
- Reading files and Writing files
- File operations: list, walk, copy, move, and delete
- App directories: app directories and user folders