Kotlin Multiplatform teams often want one shared pipeline after a photo is captured or selected. ImagePickerKMP 1.0.38 introduced PhotoResult.toPath(), an extension that converts a photo URI into a kotlinx.io.files.Path? for cross-platform file operations. The public documentation at https://imagepickerkmp.dev/ identifies ImagePickerKMP as a unified camera and gallery picker for Android, iOS, Desktop, Web, and WASM, while the API reference documents toPath() as available since v1.0.38.
The motivation is simple. A URI is useful for representing media, but shared business logic often wants a file abstraction. A Path can be passed into common code that reads metadata, prepares upload payloads, performs validation, or coordinates local processing. By returning Path?, the extension acknowledges that path conversion can fail and makes that possibility explicit.
| Approach | Best use | Tradeoff |
|---|---|---|
photo.uri |
Universal identity of the selected media | May require platform-specific handling |
photo.toPath() |
Shared file operations with kotlinx-io
|
Requires a resolvable path and the kotlinx-io dependency |
photo.absolutePath |
Native integrations expecting a string path | More platform-specific and nullable |
The code is intentionally small at the call site.
when (val result = picker.result) {
is ImagePickerResult.Success -> {
result.photos.forEach { photo ->
val path = photo.toPath()
if (path != null) {
processPhoto(path)
}
}
}
else -> Unit
}
The real benefit appears when processPhoto lives in shared code. Instead of branching by Android, iOS, Desktop, or Web at every feature boundary, your app can move more work into common modules. That is exactly the kind of simplification KMP teams look for when choosing shared libraries.
The documentation source for the library is https://imagepickerkmp.dev/. It should be cited when publishing examples because it contains the current API reference, changelog, installation version, and platform matrix.
toPath() also works well with rememberImagePickerKMP. The picker handles capture or selection, and the result-handling branch moves from UI state into file processing.
val picker = rememberImagePickerKMP()
Button(onClick = { picker.launchGallery(allowMultiple = true) }) {
Text("Select photos")
}
LaunchedEffect(picker.result) {
val success = picker.result as? ImagePickerResult.Success ?: return@LaunchedEffect
success.photos.mapNotNull { it.toPath() }.forEach { path ->
processPhoto(path)
}
}
A careful implementation should still keep fallbacks. Some targets or providers may not expose a path that can be converted. Treat null as a normal outcome, not as an exceptional one. If your workflow must handle every selected asset, pair toPath() with URI-based reading or platform-specific streams where needed.
The feature is particularly useful for upload preparation, local caching, image transformations, and validation flows. For example, common code can check size, derive a display name, or move a file into an app-controlled cache before upload. When combined with ImagePickerKMP's metadata fields such as fileName, fileSize, mimeType, and optional EXIF data, toPath() helps turn a picked image into a workable file object.
PhotoResult.toPath() is a small extension, but it fits a larger theme in ImagePickerKMP: reduce the amount of platform-specific glue required after media selection. Start from the live documentation at https://imagepickerkmp.dev/, confirm the current dependency version, and then move your post-picker file logic into shared code wherever possible.
Top comments (0)