1

我遵循这些准则来支持将键盘图像插入到我的应用程序中,这非常简单:

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 自己的示例应用程序将停止在 Pre Oreo 上运行。androidx 有关于此的任何已知问题吗?我还能做些什么来支持在 Pre 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 操作向该应用发送意图。这就是这里发生的事情。

4

0 回答 0