I followed these guidelines to support Keyboard image insertion into my app which is pretty straight forward:
val editText = object : EditText(this) {
override fun onCreateInputConnection(editorInfo: EditorInfo): InputConnection {
val ic: InputConnection = super.onCreateInputConnection(editorInfo)
EditorInfoCompat.setContentMimeTypes(editorInfo, arrayOf("image/png"))
val callback =
InputConnectionCompat.OnCommitContentListener { inputContentInfo, flags, opts ->
val lacksPermission = (flags and
InputConnectionCompat.INPUT_CONTENT_GRANT_READ_URI_PERMISSION) != 0
// read and display inputContentInfo asynchronously
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1 && lacksPermission) {
try {
inputContentInfo.requestPermission()
} catch (e: Exception) {
return@OnCommitContentListener false // return false if failed
}
}
// read and display inputContentInfo asynchronously.
// call inputContentInfo.releasePermission() as needed.
true // return true if succeeded
}
return InputConnectionCompat.createWrapper(ic, editorInfo, callback)
}
}
But this only works on Oreo+ devices. During investigation I found out that Google's own sample app stops working on pre Oreo if you migrate that app to androidx library from classic app compat library. Is there any known issue with androidx regarding this? What else I can do to support image insertion on pre Oreo devices?
Here is whats printed in logs:
W/ImageInsertUtil: Mime Type [image/png] is not acceptable when inserting the image
W/ImageInsertUtil: User tried to insert image in an app that does not support mimeType image/png.
W/ImageInsertUtil: Mime Type [image/gif] is not acceptable when inserting the image
W/ImageInsertUtil: User tried to insert image in an app that does not support image insertion for fallback mimetype image/gif.
W/ImageInsertUtil: Share intent [Intent { act=android.intent.action.SEND typ=image/png flg=0x10000001 pkg=com.xyz (has extras) }] failed to resolve in app [com.xyz]
GBoard falls back to sending intent to that app with action SEND if it fails to insert image. This is whats happening here.