You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用Commit Content API适配三星键盘IME图片传输返回null问题

三星键盘Commit Content API适配方案
  • 调整EditorInfo的MIME类型声明,三星键盘不匹配模糊通配符规则,不要只声明image/*,需要显式列出所有支持的图片格式:
override fun onCreateInputConnection(editorInfo: EditorInfo): InputConnection {
    val baseConnection = super.onCreateInputConnection(editorInfo)
    // 显式声明所有支持的图片MIME类型
    EditorInfoCompat.setContentMimeTypes(editorInfo,
        arrayOf("image/png", "image/jpg", "image/jpeg", "image/gif", "image/webp")
    )
    return InputConnectionCompat.createWrapper(baseConnection, editorInfo, commitContentListener)
}
  • 收到回调后主动申请URI读取权限,三星键盘不会默认授予应用内容URI的访问权限,必须先调用requestPermission()方法再读取资源:
private val commitContentListener = OnCommitContentListener { inputContentInfo, flags, opts ->
    // 过滤无效空回调
    val contentUri = inputContentInfo?.contentUri ?: return@OnCommitContentListener false
    // 申请读取权限
    if (flags and InputConnectionCompat.INPUT_CONTENT_GRANT_READ_URI_PERMISSION != 0) {
        try {
            inputContentInfo.requestPermission()
        } catch (e: SecurityException) {
            return@OnCommitContentListener false
        }
    }
    // 此处读取contentUri获取图片资源完成业务逻辑
    true
}
  • 增加空参数容错逻辑,部分One UI版本的三星键盘会先触发一次参数为空的无效回调,增加非空判断直接忽略即可,不要中断整体流程。

适配注意:One UI 3.1及以下版本的三星键盘仅对通过官方应用市场上架的应用开放图片上传能力,若本地调试仍返回null,可尝试将应用签名后发布到三星应用商店测试,或升级设备One UI版本验证。

内容的提问来源于stack exchange,提问作者BaconHair1282

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.02 07:57:04