Graphic keyboard support
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
Inserting an image on the keyboard requires the participation of both the IME and the application. The following sequence describes each step of the image insertion process:
When the user clicks on EditText, the editor sends a list of MIME content types that it accepts in EditorInfo.contentMimeTypes.
The IME reads a list of supported types and displays the content that the editor can accept.
When the user selects an image, the IME calls commitContent () and sends the InputContentInfo editor to the editor. Calling commitContent () is similar to calling commitText (), but for rich content. InputContentInfo contains a URI that identifies the content in the service provider. Your application can then request permission and read the contents 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); } };
Here is the complete documentation reference
https://developer.android.com/guide/topics/text/image-keyboard.html#how_it_works
Adding Image Support to IME
IMEs who want to send rich content to applications must implement the Commit Content API, as shown below:
Override onStartInput() or onStartInputView() and read the list of supported content types from the destination editor. The following snippet code shows how to check if 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) {
Freeze content for the app when users select an image. to avoid calling commitContent() when there is any text can cause the editor to lose focus. The following code snippet shows how to capture a GIF image.
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); }
Here is the complete documentation
https://developer.android.com/guide/topics/text/image-keyboard.html#adding_image_support_to_imes