我遵循these准则来支持将键盘图像插入我的应用程序,这很简单:
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)
}
}
但这仅适用于Oreo +设备。在调查过程中,我发现,如果您将该应用程序从经典应用程序兼容库迁移到androidx库,则Google自己的sample app会停止在Oreo上运行。 androidx是否有与此相关的已知问题?我还能做什么来支持在Oreo之前的设备上插入图像?
以下是日志中打印的内容:
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未能插入图片,它会退回以操作SEND向该应用发送意图。这是这里发生的事情。