1

我正在尝试为我的应用启用图像键盘支持。我遵循此链接https://developer.android.com/guide/topics/text/image-keyboard中的指南并尝试理解此代码。

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);
    }
};

正如我们所看到的,指南创建EditText然后方法@OverrideonCreateInputConnection(EditorInfo editorInfo){};

但就我而言,我的 xml 文件中已经有 EditText

<androidx.appcompat.widget.AppCompatEditText
                android:id="@+id/chat_box"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:background="@android:color/transparent"
                android:textSize="14sp"
                android:padding="6dp"
                android:hint="Type Message"
                android:maxLines="5"
                tools:ignore="Autofill,NestedWeights,TextFields"
                android:layout_gravity="center_vertical"
                android:layout_weight="1"
                />

这是我无法遵循指南中给定的代码。

我向谷歌搜索主题,希望有一个关于如何接受图像到 EditText 但没有运气的完整教程,这些网站总是登陆这个页面https://developer.android.com/guide/topics/text/image-键盘所以我决定在这里写一个问题希望得到答案。

我试过使用这段代码,但什么也没发生。

EditorInfo editorInfo = new EditorInfo();
        EditorInfoCompat.setContentMimeTypes(editorInfo,
                new String [] {
                        "image/png",
                        "image/gif",
                        "image/jpeg",
                        "image/webp"});
        AppCompatEditText chat_box =  view.findViewById(R.id.chat_box);
        InputConnection inputConnection =   chat_box.onCreateInputConnection(editorInfo);

        InputConnectionCompat.OnCommitContentListener connectionCompat = 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) {
                        Log.d("onCommitContent", "false");
                        return false; // return false if failed

                    }
                }

                Log.d("onCommitContent", "true");

                // read and display inputContentInfo asynchronously.
                // call inputContentInfo.releasePermission() as needed.

                return true;  // return true if succeeded
            }


        };

        InputConnectionCompat.createWrapper(inputConnection,editorInfo,connectionCompat);

我认为这是正确的方法,因为我在运行应用程序时没有看到任何错误,但是我的应用程序不支持图像插入似乎我的代码没有触发任何东西。

有人可以知道我的代码有什么问题吗?

更新: Logcat 结果

08-13 19:14:27.994 20366-7874/? E/MoreFutures: getOrDefault() java.util.concurrent.ExecutionException: java.lang.IllegalArgumentException: mp4 url​​ 在 ocz.a(PG:99) at ocz.get(PG:75) at abh.a(PG :13) at ofa.a(PG:34) at czb.a(PG:75) at czb.a(PG:74) at dba.a(Unknown Source) at oea.b(PG:5) at ofe。 run(PG:5) at jqm.run(PG:25) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1115) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:590 ) at java.lang.Thread.run(Thread.java:818) at jqa.run(PG:8) 原因:java.lang.IllegalArgumentException:dbo.b(PG:40) at dbb 的 mp4 url​​ 为空.a(PG:20) 在 fni.a(PG: 7) 在 gde.onClick(PG:23) 在 des.onClick(PG:4) 在 android.view.View.performClick(View.java:4781) 在 android.view.View$PerformClick.run(View.java: 19907) 在 android.os.Handler.handleCallback(Handler.java:739) 在 android.os.Handler.dispatchMessage(Handler.java:95) 在 android.os.Looper.loop(Looper.java:160) 在 android。 app.ActivityThread.main(ActivityThread.java:5541) 在 java.lang.reflect.Method.invoke(Native Method) 在 java.lang.reflect.Method.invoke(Method.java:372) 在 com.android.internal。 os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:964) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:759) 08-13 19:14:28.004 20366-20366/? W/ImageInsertUtil:插入图像 08-13 19:14:28.004 20366-20366/? W/ImageInsertUtil:commitContent 的图像插入失败,mime 类型图像/gif 08-13 19:14:28.004 20366-20366/?W/ImageInsertUtil:共享意图 [Intent { act=android.intent.action.SEND typ=image/gif flg=0x10000001 pkg=mgb.com.sdalyricsplus(有附加功能)}] 无法在应用程序中解决 [mgb.com.sdalyricsplus ] 08-13 19:14:28.044 20366-20366/? I/ImageInsertTask:已插入 044 20366-20366/? I/ImageInsertTask:已插入 044 20366-20366/? I/ImageInsertTask:已插入 https://media.tenor.com/images/a2dabdadd4abc1a5fc08417cc455f66f/tenor.gif 在 position=0 和 result=FAILURE 08-13 19:14:28.054 960-1911/? E/WindowManager:Window AppOp是45个Window Package iscom.google.android.inputmethod.latin

4

0 回答 0