File Picker¶
Installation¶
Quick Start¶
| Android | iOS |
|---|---|
![]() | ![]() |
| Desktop | Web |
|---|---|
![]() | ![]() |
val scope = rememberCoroutineScope()
val pickerLauncher = rememberFilePickerLauncher(
type = FilePickerFileType.Image,
selectionMode = FilePickerSelectionMode.Single,
onResult = { files ->
scope.launch {
files.firstOrNull()?.let { file ->
// Do something with the selected file
// You can get the ByteArray of the file
file.readByteArray()
}
}
}
)
Button(
onClick = {
pickerLauncher.launch()
},
modifier = Modifier.padding(16.dp)
) {
Text("Open File Picker")
}
Error Handling¶
Pass onError to observe failures: no app on the device can pick files, the dialog could not be shown (for example, the Desktop native library failed to load), or picked files could not be read. Cancelling is not a failure; onResult receives an empty list.
val pickerLauncher = rememberFilePickerLauncher(
type = FilePickerFileType.Pdf,
onError = { error: FilePickerException -> showMessage("Could not pick: ${error.message}") },
onResult = { files -> /* ... */ },
)
Every launch ends with exactly one onResult call, so a loading state can always be cleared there. When picking fails, onError is called first and onResult then receives the files that could be read: an empty list when none could, or the others when only some failed. Without onError, failures are not reported, but onResult still ends the launch.
Migration from 0.14.x: source code that calls
rememberFilePickerLauncherorrememberFileSaverLaunchercompiles unchanged, but a library compiled against 0.14.x that calls them must be rebuilt against this version.
Settings¶
Use FilePickerSettings to customize the dialog behavior:
val pickerLauncher = rememberFilePickerLauncher(
type = FilePickerFileType.Image,
settings = FilePickerSettings(
title = "Select an image",
initialDirectory = "/home/user/Pictures",
),
onResult = { files -> /* ... */ }
)
Or use rememberFilePickerSettings inside a @Composable to automatically recompose when settings change:
val settings = rememberFilePickerSettings(
title = dialogTitle,
initialDirectory = selectedDirectory,
)
val pickerLauncher = rememberFilePickerLauncher(
type = FilePickerFileType.Image,
settings = settings,
onResult = { files -> /* ... */ }
)
Common properties available on all platforms:
title- Dialog window titleinitialDirectory- Directory to open the dialog in (see Opening in a picked folder)
Migration from 0.14.x:
initialDirectorynow also accepts aKmpFile, so a literalinitialDirectory = nullmatches both overloads and no longer compiles. Leave the argument out instead;nullis its default. -imageRepresentationMode- Controls how iOS returns image assets (see iOS Image Representation)
Opening in a picked folder¶
To open the dialog in a folder the user picked earlier, pass the folder's KmpFile as initialDirectory. Calf hands it to each platform in the form it expects: its path on Desktop, its URI on Android (a picked folder is a content: tree URI with no path), and its URL on iOS:
var folder by remember { mutableStateOf<KmpFile?>(null) }
val folderPicker = rememberFilePickerLauncher(
type = FilePickerFileType.Folder,
onResult = { files -> folder = files.firstOrNull() },
)
val filePicker = rememberFilePickerLauncher(
type = FilePickerFileType.Pdf,
settings = rememberFilePickerSettings(initialDirectory = folder),
onResult = { files -> /* ... */ },
)
| Platform | Where initialDirectory applies |
|---|---|
| Android | The folder picker (OpenDocumentTree) |
| iOS | The document picker and the save dialog (directoryURL); the photo picker has no folders |
| Desktop | Every dialog |
| Web | Ignored, browsers cannot open a dialog in a given folder |
The String overload is still available, for example for a path typed on Desktop.
Desktop adds an additional property:
parentWindow- TheComposeWindowto attach the dialog to (see Desktop Setup)
iOS Image Representation¶
By default, the iOS photo picker transcodes images to a compatible format (e.g. HEIC → JPEG) so they can be displayed with Compose/Skia. If you need the original format (HEIC, RAW, etc.), set imageRepresentationMode to Current:
ImageRepresentationMode.Compatible(default) - Transcodes to JPEG. Recommended for displaying images with Compose.ImageRepresentationMode.Current- Returns original format. Use when you handle decoding yourself or need original quality.
This setting only affects iOS. It is ignored on other platforms.
File Types¶
FilePickerFileType controls which files the user can select:
FilePickerFileType.Image- Images onlyFilePickerFileType.Video- Videos onlyFilePickerFileType.ImageVideo- Images and videosFilePickerFileType.Audio- Audio files onlyFilePickerFileType.Document- Documents onlyFilePickerFileType.Text- Text files onlyFilePickerFileType.Pdf- PDF files onlyFilePickerFileType.All- All file typesFilePickerFileType.Folder- Folders only
Filter by MIME type:
Filter by extension:
Selection Modes¶
FilePickerSelectionMode.Single- Pick a single fileFilePickerSelectionMode.Multiple- Pick multiple files (unlimited)FilePickerSelectionMode.Multiple(maxItems = 5)- Pick up to 5 files
Limiting Selection Count¶
You can limit the number of files the user can select by passing maxItems to Multiple:
val pickerLauncher = rememberFilePickerLauncher(
type = FilePickerFileType.Image,
selectionMode = FilePickerSelectionMode.Multiple(maxItems = 5),
onResult = { files ->
// files.size will be at most 5
}
)
On Android (visual media picker) and iOS (PHPicker), the limit is enforced natively by the picker UI — the user cannot select more than maxItems files. On all other platforms and picker types, the result list is truncated to maxItems after selection.
Passing null (the default) allows unlimited selection. FilePickerSelectionMode.Multiple without parentheses is equivalent to Multiple(maxItems = null).
Desktop Setup¶
Native library cache¶
The desktop file picker relies on a small native library bundled inside the JAR. On first use it is extracted to ~/.cache/calf-filepicker/. The cached file name includes a hash of the library's content, so several applications, product flavors, or Calf versions running on the same machine never overwrite each other's copy, and an identical library that is already present is reused as is.
Packagers that place the library on the system library path (for example Conveyor) skip the extraction entirely, since the loader tries System.loadLibrary first.
macOS Dark Theme¶
The file dialog follows the application's theme. To enable dark mode support on macOS, add this JVM argument to your Gradle configuration:
compose.desktop {
application {
nativeDistributions {
macOS {
jvmArgs(
"-Dapple.awt.application.appearance=system",
)
}
}
}
}
Make sure to use the Gradle
runtask. Other run configurations (e.g.hotRun,jvmRun) may ignore these settings.
Parent Window¶
To attach the file dialog to the current window, you have three options:
Option 1: ProvideFilePickerParentWindow (recommended)¶
Wrap your content with ProvideFilePickerParentWindow to automatically provide the parent window to all file pickers in the tree:
fun main() = application {
Window(onCloseRequest = ::exitApplication) {
ProvideFilePickerParentWindow {
App()
}
}
}
Option 2: FrameWindowScope extension¶
Use the FrameWindowScope.rememberFilePickerLauncher extension, which auto-captures the window:
fun main() = application {
Window(onCloseRequest = ::exitApplication) {
val pickerLauncher = rememberFilePickerLauncher(
type = FilePickerFileType.Image,
onResult = { files -> /* ... */ }
)
}
}
Option 3: Explicit settings¶
Pass the window directly through FilePickerSettings:
val window = LocalFilePickerParentWindow.current
val pickerLauncher = rememberFilePickerLauncher(
type = FilePickerFileType.Image,
settings = FilePickerSettings(
parentWindow = window,
),
onResult = { files -> /* ... */ }
)
Extensions¶
All KmpFile extension functions work without passing a context parameter. On Android, the application context is captured automatically when using rememberFilePickerLauncher.
- Read the
ByteArrayof the file using thereadByteArrayextension function:
The
readByteArrayextension function is a suspending function, so you need to call it from a coroutine scope.It's not recommended to use
readByteArrayextension function on large files, as it reads the entire file into memory. See Streaming with kotlinx-io to read it as a stream instead. For large files, it's recommended to use the platform-specific APIs to read the file. You can read more about accessing the platform-specific APIs below.
- Check if the file exists using the
existsextension function:
- Get the file name using the
getNameextension function:
- Get the file system path using the
getPathextension function. It is a decoded path you can open directly, ornullwhen the file has no path:
| Platform | getPath() |
|---|---|
| Android | The path of a file: URI. null for the content: URIs the system pickers return; read those with readByteArray() or source(). |
| iOS | The path of the file the app can read. For a picked file that is the app's copy, for example …/tmp/calf-file-picker/…/Report Final.pdf, which keeps the picked file's name. |
| Desktop | The path of the file. |
| Web | Always null, browsers do not expose paths. |
- Get the file URI using the
toUriStringextension function, for example to store a reference to the file or to pass a picked folder as the AndroidinitialDirectory:
- Restore a file from a string returned by
toUriString(), for example one you stored to reopen a folder later. An absolute path works too:
val folder: KmpFile? = KmpFile.fromUriString(savedUri) // null if it is not a file reference, always null on Web
fromUriStringrestores the reference, not access to it. To keep a picked file or folder readable after the app restarts, store a bookmark instead.
- Keep access to a picked file or folder across app restarts with
createBookmark, and reopen it withKmpFile.fromBookmark:
// After picking a folder
val bookmark: String? = folder.createBookmark()
preferences.putString("exportFolder", bookmark)
// After the app restarts
val folder: KmpFile? = preferences.getString("exportFolder")?.let { KmpFile.fromBookmark(it) }
// When the folder is no longer needed
folder?.releaseBookmark()
| Platform | What a bookmark keeps |
|---|---|
| Android | A persisted permission for the picked content: URI. Items picked with the document or folder picker support it; createBookmark returns null for a URI whose grant is not persistable. Android limits how many persisted permissions an app holds, so release the ones you no longer need. |
| iOS | A bookmark of the picked location. The reopened file holds its security-scoped access until releaseBookmark, so a folder and the files inside it can be used through its url or path. Files reopened from the same location share one access, which ends when the last of them is released, so reopening a bookmark again does not add up. Picked files are copies the app owns, so bookmarks matter mostly for folders. |
| Desktop | The path. Sandboxed macOS apps would need security-scoped bookmarks, which Calf does not create. |
| Web | Not supported, createBookmark returns null. |
fromBookmark returns null when access was lost: the permission was released or revoked, or the item was deleted. A bookmark can go stale when the item is moved or renamed, so create a new one from the reopened file and store it each time.
- Get the size in bytes and the MIME type using the
getSizeandgetMimeTypeextension functions. Both returnnullwhen the value is unknown, andgetSizeisnullfor a folder:
val size: Long? = file.getSize()
val mimeType: String? = file.getMimeType() // e.g. "application/pdf"
On Android both come from the content provider, with the file extension as a fallback for the type. iOS and Desktop use the file system and the extension, and the web uses what the browser reports.
Migration from 0.14.x:
getPath()used to return the URI string on Android (content://…) and iOS (file:///…%20…), and the file name on Web. UsetoUriString()where you relied on the URI.
- Check if the file is a directory using the
isDirectoryextension function:
Overloads that accept a
PlatformContextparameter are still available for backward compatibility.
File Saver¶
rememberFileSaverLauncher lets the user save a file to a location of their choice. Available on all platforms.
@OptIn(ExperimentalCalfApi::class)
@Composable
fun SaveFileExample() {
val saverLauncher = rememberFileSaverLauncher(
onResult = { file ->
// file is the saved KmpFile, or null if cancelled
// In Safari and Firefox, which only download, it is the downloaded file
}
)
Button(onClick = {
saverLauncher.launch(
bytes = myByteArray,
baseName = "document",
extension = "pdf",
)
}) {
Text("Save File")
}
}
Like the picker, every save ends with exactly one onResult call. Pass onError to tell a failure apart from a cancellation: it is called first, with why the save failed (no app can save files, the source could not be read, or the chosen file could not be written), and onResult then receives null. Without onError, a failure looks like a cancellation.
var failure by remember { mutableStateOf<FilePickerException?>(null) }
val saverLauncher = rememberFileSaverLauncher(
onError = { error -> failure = error },
onResult = { file ->
// file: the saved file, or null if the user cancelled or saving failed (failure != null)
failure = null
},
)
Saving from a KmpFile¶
If you already have a KmpFile (e.g., from a file picker), you can pass it directly instead of loading bytes into memory:
This avoids loading the entire file content into memory. On each platform, the file is copied or streamed directly to the destination chosen by the user.
Platform behavior:
| Platform | Mechanism |
|---|---|
| Android | System document creation dialog (CreateDocument), typed after extension (for example application/pdf). Calf writes the source into the created document. |
| iOS | Export dialog (UIDocumentPickerViewController) that saves a copy under baseName.extension. The source is staged in the app's temporary directory and never moved; the staged copy is removed when the dialog closes. |
| Desktop | Native save dialog via rfd |
| Web | The browser's save dialog (showSaveFilePicker) in Chrome, Edge and other Chromium browsers, including Chrome on Android 132 and later; onResult receives the saved file, or null if the user cancels. Safari and Firefox have no save dialog API, so the file is downloaded where the browser's settings say, as it is when a browser refuses to show its dialog; onResult then receives the downloaded file once the download is handed to the browser. |
Safari and Firefox: a web page cannot open a save dialog in these browsers; where a download goes is the user's browser setting. To be asked for a location on every download, users can set Safari → Settings → General → File download location to Ask for each download, or turn on Always ask you where to save files in Firefox's settings. Calf's download then opens the system save panel;
onResultcannot tell whether the user kept the download.
If the source cannot be read or written, the failure is reported as described above. On Android the save sheet returns either a new, empty document, which is then deleted, or an existing file the user chose to overwrite. That file is untouched when the save fails before writing starts, for example because the source cannot be read; a failure while writing can leave it partly written, as with any in-place overwrite.
Android process death: the save dialog runs in another app, so Android may kill yours while it is open. A
KmpFilesource is restored when the result arrives; aByteArraysource is not, because it can be larger than the saved instance state allows. In that case saving fails. Save from aKmpFilewhen that matters.
rememberFileSaverLauncheris annotated with@ExperimentalCalfApi.
Platform-specific APIs¶
KmpFile is a wrapper around platform-specific APIs, you can access the native APIs for each platform using the following properties:
Android¶
iOS¶
// Persistent copy — always accessible, use for reading content
val nsUrl: NSURL = kmpFile.url
// Original picker URL — may expire due to iOS security scoping, use for metadata only
val originalNsUrl: NSURL = kmpFile.originalUrl
Note: On iOS, file picker URLs are security-scoped and may become inaccessible shortly after the picker callback returns.
urlpoints to a persistent copy in the app's temp directory that remains readable and keeps the picked file's name;getPath()returns its path.originalUrlholds the original picker URL — use it only for metadata (e.g., the original file name or URI viagetName()/toUriString()).Migration from 0.10.x:
KmpFile.urlon iOS previously referred to the original picker URL. It now points to the persistent copy. If you were usingurlfor the original file path, switch tooriginalUrl.
Desktop¶
Web¶
Streaming with kotlinx-io¶
Reading a large file with readByteArray loads it fully into memory. To stream it instead, for example to feed a zip reader or a parser, use the source() extension. It opens the file as a kotlinx-io RawSource; kotlinx-io is a dependency of calf-io, so nothing extra needs to be added:
import com.mohamedrejeb.calf.io.source
suspend fun importFile(file: KmpFile) {
file.source().buffered().use { source ->
val header = source.readByteArray(4)
// keep reading from `source` in chunks
}
}
source() is a suspending function. Like the other extensions it has an overload that takes a PlatformContext for Android code that runs outside a Calf composable. Always close the returned source.
| Platform | How the file is opened |
|---|---|
| Android | ContentResolver.openInputStream, so content URIs from the Storage Access Framework, Google Drive or Downloads stream directly. getPath() is null for these URIs, so use source() rather than opening a path yourself. |
| iOS, Desktop | From the file system through SystemFileSystem. |
| JS, Wasm | Browsers offer no synchronous file reads, so the whole file is read into memory first and served from a Buffer. |
Using okio¶
If your code is built on okio, convert the source with the official kotlinx-io-okio bridge:
Coil Extensions¶
In case you're using Coil in your project, Calf has a dedicated package that includes utilities to ease the integration between both libraries.
You can use it by adding the following dependency to your module build.gradle.kts file:
Currently, this package contains a KmpFileFetcher that you can use to let Coil know how to load a KmpFile by adding it to Coil's ImageLoader:
For more info regarding how to extend the Image Pipeline in Coil, you can read here.



