TransferableContent


Represents content that can be transferred between applications or processes.

Note; Consult platform-specific guidelines for best practices in content transfer operations.

Summary

Nested types

Defines the type of operation that a TransferableContent originates from.

Public properties

ClipEntry

The main content data, typically representing a text, image, file, or other transferable item.

Cmn
ClipMetadata

Metadata associated with the content, providing additional information or context.

Cmn
PlatformTransferableContent?

Optional platform-specific representation of the content, or additional platform-specific information, that can be used to access platform level APIs.

Cmn
TransferableContent.Source

The source from which the content originated like Keyboard, DragAndDrop, or Clipboard.

Cmn

Extension functions

TransferableContent?

Helper function to consume parts of TransferableContent in Android by splitting it to ClipData.Item parts.

android
Boolean

Returns whether this TransferableContent can provide an item with the mediaType.

Cmn
android

Public properties

clipEntry

val clipEntryClipEntry

The main content data, typically representing a text, image, file, or other transferable item.

clipMetadata

val clipMetadataClipMetadata

Metadata associated with the content, providing additional information or context.

platformTransferableContent

val platformTransferableContentPlatformTransferableContent?

Optional platform-specific representation of the content, or additional platform-specific information, that can be used to access platform level APIs.

source

val sourceTransferableContent.Source

The source from which the content originated like Keyboard, DragAndDrop, or Clipboard.

Extension functions

@ExperimentalFoundationApi
fun TransferableContent.consume(predicate: (ClipData.Item) -> Boolean): TransferableContent?

Helper function to consume parts of TransferableContent in Android by splitting it to ClipData.Item parts. Use this function in contentReceiver modifier's onReceive callback to easily separate remaining parts from incoming TransferableContent.

import androidx.compose.foundation.Image
import androidx.compose.foundation.content.MediaType
import androidx.compose.foundation.content.consume
import androidx.compose.foundation.content.contentReceiver
import androidx.compose.foundation.content.hasMediaType
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.text.BasicTextField
import androidx.compose.foundation.text.input.rememberTextFieldState
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.ImageBitmap

val state = rememberTextFieldState()
var images by remember { mutableStateOf<List<ImageBitmap>>(emptyList()) }
Column {
    Row { images.forEach { Image(bitmap = it, contentDescription = null) } }
    BasicTextField(
        state = state,
        modifier =
            Modifier.contentReceiver { transferableContent ->
                if (!transferableContent.hasMediaType(MediaType.Image)) {
                    return@contentReceiver transferableContent
                }
                val newImages = mutableListOf<ImageBitmap>()
                transferableContent
                    .consume { item ->
                        // only consume this item if we can read an imageBitmap
                        item.readImageBitmap()?.let {
                            newImages += it
                            true
                        } ?: false
                    }
                    .also { images = newImages }
            }
    )
}
Parameters
predicate: (ClipData.Item) -> Boolean

Decides whether to consume or leave the given item out. Return true to indicate that this particular item was processed here, it shouldn't be passed further down the content receiver chain. Return false to keep it in the returned TransferableContent.

Returns
TransferableContent?

Remaining parts of this TransferableContent.

hasMediaType

@ExperimentalFoundationApi
fun TransferableContent.hasMediaType(mediaType: MediaType): Boolean

Returns whether this TransferableContent can provide an item with the mediaType.