我正在我的应用程序中使用来自google的gboard,当我从键盘应用程序向我的EditText
输入gif时,它会显示toast
“文本字段不支持从键盘插入gif”
我找了一千遍都找不到结果
任何帮助都将不胜感激。
最佳答案
图像键盘支持
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.
How it works
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
向IMES添加图像支持
要向应用程序发送丰富内容的IME必须实现
提交内容API,如下所示:
覆盖
onStartInput()
或onStartInputView()
并读取目标编辑器中支持的内容类型。以下代码
snippet演示如何检查目标编辑器是否接受gif
图像。
@Override
public void onStartInputView(EditorInfo info, boolean restarting) {
String[] mimeTypes = EditorInfoCompat.getContentMimeTypes(editorInfo);
boolean gifSupported = false;
for (String mimeType : mimeTypes) {
if (ClipDescription.compareMimeTypes(mimeType, "image/gif")) {
gifSupported = true;
}
}
if (gifSupported) {
// the target editor supports GIFs. enable corresponding content
} else {
// the target editor does not support GIFs. disable corresponding content
}
}
当用户选择图像时,将内容提交到应用程序。避免
当有任何撰写文本时调用
commitContent()
,因为它可能会导致编辑器失去焦点。下面的代码片段显示
如何提交gif图像。
/**
* Commits a GIF image
*
* @param contentUri Content URI of the GIF image to be sent
* @param imageDescription Description of the GIF image to be sent
*/
public static void commitGifImage(Uri contentUri, String imageDescription) {
InputContentInfoCompat inputContentInfo = new InputContentInfoCompat(
contentUri,
new ClipDescription(imageDescription, new String[]{"image/gif"}));
InputConnection inputConnection = getCurrentInputConnection();
EditorInfo editorInfo = getCurrentInputEditorInfo();
Int flags = 0;
if (android.os.Build.VERSION.SDK_INT >= 25) {
flags |= InputConnectionCompat.INPUT_CONTENT_GRANT_READ_URI_PERMISSION;
}
InputConnectionCompat.commitContent(
inputConnection, editorInfo, inputContentInfo, flags, opts);
}
这是完整的文件
https://developer.android.com/guide/topics/text/image-keyboard.html#adding_image_support_to_imes