Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

342
Visualizações
How can I receive GIF's from the keyboard in jetpack compose?

I am trying to make a chat in jetpack compose, and I want to be able to use the standard gif keyboard on my samsung to send a gif.

When I click a GIF on a normal TextField, I am currently getting a message "Can't enter this content here"

I found something called Commit Content API which should make it possible to add a GIF in a old EditText so I am trying that inside an AndroidView, Now I dont get the error message anymore, But I also don't have a clue where the GIF is and how it is represented.

AndroidView(factory = {
    val editText = @SuppressLint("AppCompatCustomView")
    object : EditText(it) {

        override fun setOnReceiveContentListener(
            mimeTypes: Array<out String>?,
            listener: OnReceiveContentListener?
        ) {
            super.setOnReceiveContentListener(mimeTypes, listener)
        }


        override fun onCreateInputConnection(editorInfo: EditorInfo): InputConnection {
            val ic: InputConnection = super.onCreateInputConnection(editorInfo)
            EditorInfoCompat.setContentMimeTypes(editorInfo, arrayOf("image/gif"))

            val callback =
                InputConnectionCompat.OnCommitContentListener { inputContentInfo, _, _ ->
                    try {
                        inputContentInfo.requestPermission()
                    } catch (e: Exception) {
                        return@OnCommitContentListener false
                    }
                    true  // return true if succeeded
                }
            return InputConnectionCompat.createWrapper(ic, editorInfo, callback)
        }
    }
    editText
}) {}
over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

callback is called each time a GIF is selected. You can get the URI from inputContentInfo:

val callback =
    InputConnectionCompat.OnCommitContentListener { inputContentInfo, _, _ ->
        try {
            inputContentInfo.requestPermission()
        } catch (e: Exception) {
            return@OnCommitContentListener false
        }
        gifUri = inputContentInfo.contentUri
        true  // return true if succeeded
    }

Perfectly, you should copy the file from this URI into your own storage and call inputContentInfo.releasePermission(), because contentUri will be freed at some point. More info can be found in the documentation.

You can display the content of this URI using Coil as showed in this answer. Note that you need to add the dependency io.coil-kt:coil-gif:$coil_version

A full working example:

var gifUri by remember { mutableStateOf<Uri?>(null) }
val context = LocalContext.current
Image(
    rememberImagePainter(
        gifUri,
        imageLoader = remember {
            ImageLoader(context).newBuilder()
                .componentRegistry {
                    if (SDK_INT >= 28) {
                        add(ImageDecoderDecoder(context))
                    } else {
                        add(GifDecoder())
                    }
                }.build()
        }
    ),
    contentDescription = null,
    modifier = Modifier.size(300.dp)
)

AndroidView(factory = { context ->
    val editText = @SuppressLint("AppCompatCustomView")
    object : EditText(context) {

        override fun setOnReceiveContentListener(
            mimeTypes: Array<out String>?,
            listener: OnReceiveContentListener?
        ) {
            super.setOnReceiveContentListener(mimeTypes, listener)
        }


        override fun onCreateInputConnection(editorInfo: EditorInfo): InputConnection {
            val ic: InputConnection = super.onCreateInputConnection(editorInfo)
            EditorInfoCompat.setContentMimeTypes(editorInfo, arrayOf("image/gif"))

            val callback =
                InputConnectionCompat.OnCommitContentListener { inputContentInfo, _, _ ->
                    try {
                        inputContentInfo.requestPermission()
                    } catch (e: Exception) {
                        return@OnCommitContentListener false
                    }
                    gifUri = inputContentInfo.contentUri
                    true  // return true if succeeded
                }
            return InputConnectionCompat.createWrapper(ic, editorInfo, callback)
        }
    }
    editText
}) {}
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda