我正在尝试为我的应用启用图像键盘支持。我遵循了来自此链接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
方法,然后创建了@Override
方法,onCreateInputConnection(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"
/>
我无法遵循指南中的给定代码。
我在google上搜索了该主题,希望有一个完整的教程介绍如何将图像接受到EditText,但是没有运气,这些站点总是登陆到此页面https://developer.android.com/guide/topics/text/image-keyboard,所以我决定在此处写一个问题,希望一个答案。
我尝试使用此代码,但没有任何反应。
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网址为空 在ocz.a(PG:99) 在ocz.get(PG:75) 在abh.a(PG:13) 在ofa.a(PG:34) 在czb.a(PG:75) 在czb.a(PG:74) 在dba.a(未知来源) 在oea.b(PG:5) 在ofe.run(PG:5) 在jqm.run(PG:25) 在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1115) 在java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:590) 在java.lang.Thread.run(Thread.java:818) 在jqa.run(PG:8) 造成原因:java.lang.IllegalArgumentException:mp4网址为空 在dbo.b(PG:40) 在dbb.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(本机方法) 在java.lang.reflect.Method.invoke(Method.java:372) 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:964) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:759)08-13 19:14:28.004 20366-20366 /? W / ImageInsertUtil:Mime类型[image / gif]是 插入图像时不可接受08-13 19:14:28.004 20366-20366 /? W / ImageInsertUtil:图片插入失败 具有mime类型image / gif的commitContent 08-13 19:14:28.004 20366-20366 /? W / ImageInsertUtil:共享意图[意图{ 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:已插入 https://media.tenor.com/images/a2dabdadd4abc1a5fc08417cc455f66f/tenor.gif 位置= 0,结果= FAILURE 08-13 19:14:28.054 960-1911 /? E / WindowManager:Window AppOp是45 Window Package iscom.google.android.inputmethod.latin