Images doesn't send to Image Keyboard

105
February 21, 2018, at 6:17 PM

Hi I have been trying to send image,gif or webap to IME Keyboard. So here I have created the separate custom keyboard which contains rich content like gif,webap and jpg images that are going to be send from my keyboard to target app, everything is working good but images doesn't sent to that target app's keyboard. I have used the InputConnectionCompat.commitContent() Api to send. I don't know what is the issues and why which is not sending rich content to keyboard, could you clarify this?

My code part is:

  private void commitTyped(InputConnection inputConnection, String imgUrl) {
    if (imgUrl.length() > 0) {
        /*inputConnection.commitText(imgUrl, imgUrl.length());*/
        if (isCommitContentSupported(MIME_TYPE_GIF)
                || isCommitContentSupported(MIME_TYPE_PNG)
                || isCommitContentSupported(MIME_TYPE_WEBP)
                || isCommitContentSupported(MIME_TYPE_JPG)) {
            String mime;
            if (imgUrl.contains("gif"))
                mime = MIME_TYPE_GIF;
            else if (imgUrl.contains("webp"))
                mime = MIME_TYPE_WEBP;
            else if (imgUrl.contains("jpg") || imgUrl.contains("jpeg"))
                mime = MIME_TYPE_JPG;
            else
                mime = MIME_TYPE_PNG;
            doCommitContent("", MIME_TYPE_JPG, new File(Constants.sampleFilePath));
        } else {
            inputConnection.commitText(imgUrl, imgUrl.length());
        }
        mComposing.setLength(0);
        //updateCandidates();
    }
}
 private void doCommitContent(@NonNull String description, @NonNull String mimeType, @NonNull File file) {
    final EditorInfo editorInfo = getCurrentInputEditorInfo();
    // Validate packageName again just in case.
    if (!validatePackageName(editorInfo)) {
        return;
    }
    /*final Uri contentUri = FileProvider.getUriForFile(this, AUTHORITY, file);*/
    final Uri contentUri = Uri.parse(file.toString());
    // As you as an IME author are most likely to have to implement your own content provider
    // to support CommitContent API, it is important to have a clear spec about what
    // applications are going to be allowed to access the content that your are going to share.
    final int flag;
    if (Build.VERSION.SDK_INT >= 25) {
        // On API 25 and later devices, as an analogy of Intent.FLAG_GRANT_READ_URI_PERMISSION,
        // you can specify InputConnectionCompat.INPUT_CONTENT_GRANT_READ_URI_PERMISSION to give
        // a temporary read access to the recipient application without exporting your content
        // provider.
        flag = InputConnectionCompat.INPUT_CONTENT_GRANT_READ_URI_PERMISSION;
    } else {
        // On API 24 and prior devices, we cannot rely on
        // InputConnectionCompat.INPUT_CONTENT_GRANT_READ_URI_PERMISSION. You as an IME author
        // need to decide what access control is needed (or not needed) for content URIs that
        // you are going to expose. This sample uses Context.grantUriPermission(), but you can
        // implement your own mechanism that satisfies your own requirements.
        flag = 0;
        try {
            // TODO: Use revokeUriPermission to revoke as needed.
            grantUriPermission(
                    editorInfo.packageName, contentUri, Intent.FLAG_GRANT_READ_URI_PERMISSION);
        } catch (Exception e) {
            Log.e(TAG, "grantUriPermission failed packageName=" + editorInfo.packageName
                    + " contentUri=" + contentUri, e);
        }
    }
    final InputContentInfoCompat inputContentInfoCompat = new InputContentInfoCompat(contentUri,
            new ClipDescription("ImgeOrGif", new String[]{mimeType}), null /* linkUrl */);
    InputConnectionCompat.commitContent(
            getCurrentInputConnection(), getCurrentInputEditorInfo(), inputContentInfoCompat,
            flag, null);
}

private boolean isCommitContentSupported(@NonNull String mimeType) {
    if (getCurrentInputEditorInfo() == null) {
        return false;
    }
    final InputConnection ic = getCurrentInputConnection();
    if (ic == null) {
        return false;
    }
    if (!validatePackageName(getCurrentInputEditorInfo())) {
        return false;
    }
    final String[] supportedMimeTypes = EditorInfoCompat.getContentMimeTypes(getCurrentInputEditorInfo());
    for (String supportedMimeType : supportedMimeTypes) {
        if (ClipDescription.compareMimeTypes(mimeType, supportedMimeType)) {
            return true;
        }
    }
    return false;
}
READ ALSO
Android - byte array copy for frames in mobile vision text recognisation

Android - byte array copy for frames in mobile vision text recognisation

I am trying the run text recognizer only on a cropped portion of the frame from mobile vision API

138
Firebase Storage uploaded Videos to recycler view in Android

Firebase Storage uploaded Videos to recycler view in Android

I Had uploaded Video Files Directly to Firebase Storage via Console

116
How can i get permission callback in adapter

How can i get permission callback in adapter

I have an ArrayAdapter where I need to pick images

122
Binding to JobService not possible - alternatives?

Binding to JobService not possible - alternatives?

While migrating from Android's Service to JobService to meet changes for Oreo I noticed that onBind is now final (see: https://androidgooglesource

92