Gboard:在EditText上启用GIF插入 [英] Gboard: Enable GIF insertion on EditText

查看:104
本文介绍了Gboard:在EditText上启用GIF插入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在我的应用程序中使用Google的 Gboard ,当我从键盘应用程序向EditText输入 GIF 时,它会显示吐司

I am using the Gboard from Google in my app and when i input a GIF from the keyboard app to my EditText it then shows a toast

文本字段不支持从键盘插入GIF"

"The text field does not support GIF insertion from the keyboard"

我已经搜索了上千次,却找不到结果

I have search about this a thousand times and can't find out a result

任何帮助将不胜感激.

推荐答案

图像键盘支持


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和 该应用程序.以下序列描述了图像中的每个步骤 插入过程:

Keyboard image insertion requires participation from both the IME and the app. The following sequence describes each step in the image insertion process:

当用户点击EditText时,编辑器将发送MIME列表 它在EditorInfo.contentMimeTypes中接受的内容类型.

When the user taps on an EditText, the editor sends a list of MIME content types that it accepts in EditorInfo.contentMimeTypes.

IME读取支持的类型列表,并在 编辑器可以接受的软键盘.

The IME reads the list of supported types and displays content in the soft keyboard that the editor can accept.

当用户选择图像时,IME会调用commitContent()并 将InputContentInfo发送到编辑器. commitContent()调用是 与commitText()调用类似,但内容丰富. InputContentInfo包含一个URI,用于标识内容中的内容. 内容提供商.然后,您的应用可以请求权限并阅读 URI中的内容.

When the user selects an image, the IME calls commitContent() and sends an InputContentInfo to the editor. The commitContent() call is analogous to the commitText() call, but for rich content. InputContentInfo contains an URI that identifies the content in a content provider. Your app can then request permission and read the content from the 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/topic/text/image-keyboard.html#how_it_works

想要向应用程序发送丰富内容的

IME必须实现 提交内容API,如下所示:

IMEs that want to send rich content to apps must implement the Commit Content API as shown below:

覆盖onStartInput()onStartInputView()并读取以下内容的列表 目标编辑器支持的内容类型.以下代码 片段显示如何检查目标编辑器是否接受GIF 图片.

Override onStartInput() or onStartInputView() and read the list of supported content types from the target editor. The following code snippet shows how to check whether the target editor accepts GIF images.

@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图片.

Commit content to the app when the users selects an image. Avoid calling commitContent() when there is any composing text because it might cause the editor to lose focus. The following code snippet shows how to commit a GIF image.

/**
 * 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/topic/text/image-keyboard.html#adding_image_support_to_imes

这篇关于Gboard:在EditText上启用GIF插入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆