I am trying to enable the Image keyboard support to my app. I followed the guide from this link https://developer.android.com/guide/topics/text/image-keyboard and trying to understand this code.
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);
}
};
As we can see, The guide create EditText
and then @Override
the onCreateInputConnection(EditorInfo editorInfo){};
method
but in my case, I already have EditText from my xml file
<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"
/>
which is I can't follow the given code from the guide.
I search the topic to google hoping that there is a full tutorial on how to accept the image to EditText but no luck, the sites always landed to this page https://developer.android.com/guide/topics/text/image-keyboard so I decided to write a question here hoping for an answer.
I have tried using this code but nothing happened.
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);
I thought this is the correct way because I don't see any errors when I ran the app but still, my app doesn't support image insertion seems that my code did not trigger anything.
Can someone knows what is the problem of my code?
Update: Logcat result
08-13 19:14:27.994 20366-7874/? E/MoreFutures: getOrDefault() java.util.concurrent.ExecutionException: java.lang.IllegalArgumentException: the mp4 url is empty at 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) Caused by: java.lang.IllegalArgumentException: the mp4 url is empty at dbo.b(PG:40) at dbb.a(PG:20) at fni.a(PG:7) at gde.onClick(PG:23) at des.onClick(PG:4) at android.view.View.performClick(View.java:4781) at android.view.View$PerformClick.run(View.java:19907) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:160) at android.app.ActivityThread.main(ActivityThread.java:5541) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at 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: Mime Type [image/gif] is not acceptable when inserting the image 08-13 19:14:28.004 20366-20366/? W/ImageInsertUtil: Image insertion failed for commitContent with mime-type image/gif 08-13 19:14:28.004 20366-20366/? W/ImageInsertUtil: Share intent [Intent { act=android.intent.action.SEND typ=image/gif flg=0x10000001 pkg=mgb.com.sdalyricsplus (has extras) }] failed to resolve in app [mgb.com.sdalyricsplus] 08-13 19:14:28.044 20366-20366/? I/ImageInsertTask: Inserted https://media.tenor.com/images/a2dabdadd4abc1a5fc08417cc455f66f/tenor.gif in position=0 and result=FAILURE 08-13 19:14:28.054 960-1911/? E/WindowManager: Window AppOp is 45 Window Package iscom.google.android.inputmethod.latin