Hey folks, I'm having trouble with GIF/Image inser...
# compose
j
Hey folks, I'm having trouble with GIF/Image insertion using
BasicTextField
(formerly
BasicTextField2
) and the
contentReceiver()
Modifier in Compose 1.7-beta05 on a Samsung, API 34 device (Samsung SM-F926U1). It works great on my Pixel 6 (API 34). I'm worried that it may not work on more Samsungs, though I don't currently have one to test on. Issue: The `ReceiveContentListener`'s
onReceive
is never called when selecting in a GIF (or any type of image) from the Samsung Keyboard. Thus the "Can't enter this content here." toast shows. Our prior implementation, with
AppCompatEditText
wrapped in an
AndroidView
and overriding
onCreateInputConnection
works on this device in question. I'm migrating from a wrapped EditText/AndroidView combo to full compose to solve a separate ANR issue with focus requesting, and this is understandably a blocker, because we don't want our users to lose Image insertion in the chat experience.
The code is rather simple, I believe:
Copy code
BasicTextField(
    state = state,
    enabled = enabled,
    keyboardOptions = KeyboardOptions(
        capitalization = KeyboardCapitalization.Sentences,
        keyboardType = KeyboardType.Text,
    ),
    modifier = Modifier
        .fillMaxWidth()
        .contentReceiver { transferableContent ->
            if (!transferableContent.hasMediaType(MediaType.Image)) {
                return@contentReceiver transferableContent
            }
            transferableContent.consume {
                it.uri?.let { uri ->
                    onMediaSelected(uri)
                    true
                } ?: false
            }
        }
        .focusRequester(focusRequester),
    )
@Halil Ozercan @Zach Klippenstein (he/him) [MOD] Apologies for the ping, but based on PR history you seem to be experts with this API.
h
Can you please file a bug? I could reproduce the issue in Compose Demo app on another Samsung device.
j
Will do!
h
Thanks for bringing this to our attention. It seems like this is due to the fact that we pass
*/*
as accepted mime type for the input connection. This was previously a non-documented behavior on Android platform and should have been avoided by apps. Gboard works fine in this situation but Samsung keyboard doesn't like it. If we simply expand the mime types list to
*/*, image/*
, then gifs work.
In the first draft of the
contentReceiver
API we presented this choice (configuring accepted mime types) to developers but ultimately decided to scrap it since it had almost no use other than hinting at the IME. Apparently it can still be useful.
❤️ 1
j
Gotcha, thanks for responding and debugging so quickly!