Add an image to the Messages app on Android

I am trying to create a custom keyboard and use the "SoftKeyboard" sample in the Android SDK. I made several modifications with this sample and created my own keyboard. I can use this keyboard custome with the default messaging app of my Android device.

Now I want to press a button on my user keyboard and add an image when entering SMS. I noticed that there is a String Builder class in the "SoftKeyBoard.java" class ( private StringBuilder mComposing = new StringBuilder() ), and it is added to characters when entering letters using the keyboard.

I tried adding an image of my SD card as below,

  String imageDataString = ""; String path = Environment.getExternalStorageDirectory().toString() + "/SamplePictures/"; File file = new File(path, "myimage.jpg"); try { FileInputStream imageInFile = new FileInputStream(file); byte imageData[] = new byte[(int) file.length()]; imageInFile.read(imageData); // Converting Image byte array into Base64 String imageDataString = encodeImage(imageData); imageInFile.close(); } catch (FileNotFoundException e) { System.out.println("Image not found" + e); } catch (IOException ioe) { System.out.println("Exception while reading the Image " + ioe); } 

and I added "imageDataString" to the line builder as shown below,

 mComposing.append(imageDataString); 

But I have so many characters, not an image. Can I insert an image when entering SMS using the keyboard?

Updated: I used ImageSpan and Spannable with the following code.

 SpannableStringBuilder ssb = new SpannableStringBuilder( "Here a my picture " ); Bitmap smiley = BitmapFactory.decodeResource( getResources(), R.drawable.bitmap ); ssb.setSpan( new ImageSpan( smiley ), 16, 17,Spannable.SPAN_INCLUSIVE_INCLUSIVE ); mComposing.append(ssb); 

But it only displays β€œHere is my photo” and no image. I created a sample standalone application with EditText and set the "ssb" variable above as the text for this EditText. Then it displays the image well. But this does not work with the Messaging app. If I can install the EditText messaging app, I think I can install the image.

Is there a way to access and update the edit text of the messaging application? Thanks at Advance .. !!

+4
source share
1 answer

I think you want to use ImageSpan added to Spannable , a solution that is already described here . After clicking the image button on the keyboard, you will run the method, which should update editText , taking the existing text from it, adding the ImageSpan containing your image, and setting it back to editText .

+1
source

All Articles