1

I have a custom keyboard in my application which have full of images When I click the image I want to send the image to the input connection(i.e want to show the image in default edit text of the application)How to pass the image when the image is clicked ??Please help

jagan
  • 39
  • 3

2 Answers2

1

Use InputConnectionCompat something like this:

private void commitImage(Uri contentUri) {
    InputContentInfoCompat inputContentInfo = new InputContentInfoCompat(contentUri, new ClipDescription("", new String[]{"image/jpg"}), null);
    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, null);
}

on kotlin:

private fun commitImage(contentUri: Uri) {
    val inputContentInfo =
        InputContentInfoCompat(contentUri, ClipDescription("", arrayOf("image/jpg")), null)
    val inputConnection = currentInputConnection
    val editorInfo = currentInputEditorInfo
    var flags = 0
    if (Build.VERSION.SDK_INT >= 25) {
        flags = flags or InputConnectionCompat.INPUT_CONTENT_GRANT_READ_URI_PERMISSION
    }
    InputConnectionCompat.commitContent(
        inputConnection,editorInfo,inputContentInfo,flags,null)
}

source code here

Zainal Fahrudin
  • 536
  • 4
  • 23
0

In short, it's not possible. I had a job awhile back where I was trying to essentially do that exact thing, and I found that it's only possible to pass images if they are unicode characters. I further found that I could only pass certain ones on pre-Lollipop devices, because they didn't have the full set.

If you're only looking to deploy to Lollipop+ and your images are unicode, you can use getCurrentInputConnection().commitText("\\u2602", 1); for an umbrella like this ☂

LukeWaggoner
  • 8,869
  • 1
  • 29
  • 28
  • idratherbeintheair Thanks for the information,But can you please tell how to convert the bitmap image to Unicode characters?? – jagan Nov 25 '15 at 06:13
  • You can't convert something to unicode. Unicode characters are a set list of characters that were defined by the Unicode consortium, formed in 1991. Different version of it have come out from time to time, and the latest version is 8.0 which came out in June 2015. This is a full list of the characters. http://unicode-table.com/en/#control-character – LukeWaggoner Nov 25 '15 at 15:35