pasteImageFromClipboard

actual fun pasteImageFromClipboard(onLoaded: (bytes: ByteArray, intrinsicSize: Size) -> Unit)

Android clipboard paste. Reads ClipboardManager.primaryClip for an image URI (covers Files / Gallery / Photos "Copy" flows plus browser "Copy image" on most Chromium derivatives), resolves it through the application ContentResolver, and delivers the bytes + intrinsic pixel size on the main thread.

Targets tablets / Chromebooks / DeX where Cmd+V is a real flow, and also covers a host-supplied "paste" toolbar button on phones. Plain phones with software keyboards never hit Cmd+V, but a paste button binds to the same function so the implementation is reused.

Why reflection for context

The expect signature is a plain top-level function, not @Composable — it's called from onPreviewKeyEvent, which itself isn't @Composable. Reaching LocalContext.current from there is not possible without API-level changes that cascade through every actual

  • the host call site. ActivityThread.currentApplication() is a private-but-stable Android internal that returns the live Application instance; this is the canonical pattern used by LeakCanary, ProcessLifecycleOwner, Coil, and others for the same reason. If R8 strips ActivityThread, the call returns null and paste no-ops gracefully.

expect fun pasteImageFromClipboard(onLoaded: (bytes: ByteArray, intrinsicSize: Size) -> Unit)

Read a single image from the platform clipboard, if one is present.

Async because:

  • Web's navigator.clipboard.read() is Promise-based and the browser may prompt for permission on first call.

  • Desktop AWT Toolkit.systemClipboard.getData(DataFlavor.imageFlavor) returns a BufferedImage we need to re-encode to PNG bytes on a background thread.

Invokes onLoaded on the main thread when an image is found and decoded to encoded bytes (PNG / JPEG / WebP — whatever the clipboard contains, re-encoded to PNG on platforms where the clipboard owns a decoded bitmap). Silently no-ops when:

  • Clipboard is empty.

  • Clipboard holds non-image content (text, files, etc.).

  • Permission is denied (Web) or the clipboard is unavailable.

Per-platform implementations:

  • JVM (Desktop): AWT Toolkit.systemClipboard + DataFlavor.imageFlavor, re-encoded to PNG via ImageIO.write.

  • Web (WasmJS / JS): navigator.clipboard.read() → ClipboardItem → getType("image/png") → blob → bytes.

  • Android: ClipboardManager.primaryClip → image URI → ContentResolver → PNG bytes (off the UI thread). Targets the tablet / Chromebook / DeX hardware-keyboard Cmd+V flow; a host toolbar button can bind to the same call on phones.

  • iOS: UIPasteboard.general.image re-encoded via UIImagePNGRepresentation. Wired primarily for iPad hardware keyboards (Cmd+V); phones go through a host toolbar button.

actual fun pasteImageFromClipboard(onLoaded: (bytes: ByteArray, intrinsicSize: Size) -> Unit)

iOS clipboard paste — reads UIPasteboard.general for an image and delivers the encoded bytes plus intrinsic pixel size. Wired primarily for iPad with a hardware keyboard where Cmd+V is the natural paste shortcut; iPhone users without keyboards trigger this through a host-supplied toolbar button instead.

Re-encoded to PNG via UIImagePNGRepresentation so the SDK's decoder sees a known container regardless of what the source app put on the pasteboard (could be PNG, JPEG, TIFF, or raw pixel data).

actual fun pasteImageFromClipboard(onLoaded: (bytes: ByteArray, intrinsicSize: Size) -> Unit)

Legacy Kotlin/JS implementation. Stays in fully-dynamic territory because Kotlin/JS's standard Promise<T> doesn't surface chained dynamic types cleanly through the .then() overloads — using js("...") keeps the Async Clipboard call chain in JS land where the API was designed.

actual fun pasteImageFromClipboard(onLoaded: (bytes: ByteArray, intrinsicSize: Size) -> Unit)

Read a single image from the platform clipboard, if one is present.

Async because:

  • Web's navigator.clipboard.read() is Promise-based and the browser may prompt for permission on first call.

  • Desktop AWT Toolkit.systemClipboard.getData(DataFlavor.imageFlavor) returns a BufferedImage we need to re-encode to PNG bytes on a background thread.

Invokes onLoaded on the main thread when an image is found and decoded to encoded bytes (PNG / JPEG / WebP — whatever the clipboard contains, re-encoded to PNG on platforms where the clipboard owns a decoded bitmap). Silently no-ops when:

  • Clipboard is empty.

  • Clipboard holds non-image content (text, files, etc.).

  • Permission is denied (Web) or the clipboard is unavailable.

Per-platform implementations:

  • JVM (Desktop): AWT Toolkit.systemClipboard + DataFlavor.imageFlavor, re-encoded to PNG via ImageIO.write.

  • Web (WasmJS / JS): navigator.clipboard.read() → ClipboardItem → getType("image/png") → blob → bytes.

  • Android: ClipboardManager.primaryClip → image URI → ContentResolver → PNG bytes (off the UI thread). Targets the tablet / Chromebook / DeX hardware-keyboard Cmd+V flow; a host toolbar button can bind to the same call on phones.

  • iOS: UIPasteboard.general.image re-encoded via UIImagePNGRepresentation. Wired primarily for iPad hardware keyboards (Cmd+V); phones go through a host toolbar button.

actual fun pasteImageFromClipboard(onLoaded: (bytes: ByteArray, intrinsicSize: Size) -> Unit)

Read a single image from the platform clipboard, if one is present.

Async because:

  • Web's navigator.clipboard.read() is Promise-based and the browser may prompt for permission on first call.

  • Desktop AWT Toolkit.systemClipboard.getData(DataFlavor.imageFlavor) returns a BufferedImage we need to re-encode to PNG bytes on a background thread.

Invokes onLoaded on the main thread when an image is found and decoded to encoded bytes (PNG / JPEG / WebP — whatever the clipboard contains, re-encoded to PNG on platforms where the clipboard owns a decoded bitmap). Silently no-ops when:

  • Clipboard is empty.

  • Clipboard holds non-image content (text, files, etc.).

  • Permission is denied (Web) or the clipboard is unavailable.

Per-platform implementations:

  • JVM (Desktop): AWT Toolkit.systemClipboard + DataFlavor.imageFlavor, re-encoded to PNG via ImageIO.write.

  • Web (WasmJS / JS): navigator.clipboard.read() → ClipboardItem → getType("image/png") → blob → bytes.

  • Android: ClipboardManager.primaryClip → image URI → ContentResolver → PNG bytes (off the UI thread). Targets the tablet / Chromebook / DeX hardware-keyboard Cmd+V flow; a host toolbar button can bind to the same call on phones.

  • iOS: UIPasteboard.general.image re-encoded via UIImagePNGRepresentation. Wired primarily for iPad hardware keyboards (Cmd+V); phones go through a host toolbar button.