图像键盘支持
Users often want to communicate with emojis, stickers, and other kinds of rich
content. In previous versions of Android, soft keyboards (also known as input
method editors or IMEs) could send only unicode emoji to apps. For rich
content, apps had to either build app-specific APIs that couldn't be used in
other apps or use workaround like sending images through Easy Share Action or the
clipboard.
怎么运行的
键盘图像插入需要 IME 和
该应用程序。以下序列描述了图像中的每个步骤
插入过程:
当用户点击 EditText 时,编辑器会发送在 EditorInfo.content MimeTypes 中接受的 MIME 内容类型列表。
IME 读取支持的类型列表并在
编辑器可以接受的软键盘。
当用户选择图像时,IME 调用 commitContent() 并
将 InputContentInfo 发送到编辑器。 commitContent() 调用是
类似于 commitText() 调用,但内容丰富。
InputContentInfo 包含一个 URI,用于标识 a 中的内容
内容提供商。然后,您的应用程序可以请求权限并读取
来自 URI 的内容。
To accept rich content from IMEs, apps must tell IMEs what content types it
accepts and specify a callbackup method that is executed when content is
received. The following example demonstrates how to create an EditText that
accept PNG images:
EditText editText = new EditText(this) {
@Override
public InputConnection onCreateInputConnection(EditorInfo editorInfo) {
final InputConnection ic = super.onCreateInputConnection(editorInfo);
EditorInfoCompat.setContentMimeTypes(editorInfo,
new String [] {"image/png"});
final InputConnectionCompat.OnCommitContentListener callback =
new InputConnectionCompat.OnCommitContentListener() {
@Override
public boolean onCommitContent(InputContentInfoCompat inputContentInfo,
int flags, Bundle opts) {
// read and display inputContentInfo asynchronously
if (BuildCompat.isAtLeastNMR1() && (flags &
InputConnectionCompat.INPUT_CONTENT_GRANT_READ_URI_PERMISSION) != 0) {
try {
inputContentInfo.requestPermission();
}
catch (Exception e) {
return false; // return false if failed
}
}
// read and display inputContentInfo asynchronously.
// call inputContentInfo.releasePermission() as needed.
return true; // return true if succeeded
}
};
return InputConnectionCompat.createWrapper(ic, editorInfo, callback);
}
};
这是完整的文档参考
https://developer.android.com/guide/topics/text/image-keyboard.html#how_it_works https://developer.android.com/guide/topics/text/image-keyboard.html#how_it_works
为 IME 添加图像支持
想要向应用程序发送丰富内容的 IME 必须实现
提交Content API如下所示:
覆盖onStartInput()
or onStartInputView()
并阅读列表
目标编辑器支持的内容类型。下面的代码
代码片段显示如何检查目标编辑器是否接受 GIF
图片。