5

I am trying to create a custom keyboard and use "SoftKeyboard" sample in android SDK for it. I did few modifications with that sample and created my custom keyboard. I can use this custome keyboard with default Messaging app of my Android device.

Now I want to click a button in my custom keyboard and add an image when I type a SMS. I noticed that there is String Builder in "SoftKeyBoard.java" class (private StringBuilder mComposing = new StringBuilder()) and it is appended chars when we type letters using keyboard.

I tried to append an image of my SD card like 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 appended "imageDataString" to String builder like below,

mComposing.append(imageDataString);

But I got so many characters, not an image. Is it possible to insert an image when I type SMS using my keyboard?

Updated : I used ImageSpan and Spannable with following code.

SpannableStringBuilder ssb = new SpannableStringBuilder( "Here's 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 displays only "Here's a my picture" and no image. I created a sample separate app with an EditText and set above "ssb" variable as the text of that EditText. Then it displays well the image. But it doesn't work with Messaging app. If I can set the Messaging app EditText, I guess I can set the image.

Is there any way to access and update the Edit text of Messaging app? Thanks in Advance..!!

harsh
  • 2,399
  • 9
  • 29
  • 47

1 Answers1

1

I think what you want to do is use an ImageSpan added to a Spannable, a solution which is already described here. After pressing the image button on your keyboard, you'll fire of a method that should update the editText by taking the existing text from it, adding an ImageSpan containing your image and setting that back to the editText.

Community
  • 1
  • 1
Aert
  • 1,989
  • 2
  • 15
  • 17
  • 1
    Hey Aert, Thanks for your answer. I tried with your suggestion before. Please find the updated part of my question. Please let me know your thoughts. Can you try this with your real device's messaging app? Some times there may be an issue with my device. Thanks again – harsh Feb 27 '13 at 17:34
  • Sorry to say I don't think that's possible. You (your keyboard) doesn't own the edittext, the application does. The app will determine how things are displayed, and will have to have access to the image resources to display. You'll need to implement your own messaging app, or perhaps there are even some that take in an image URL tag and automatically replace it with an image... But it definitely wouldn't work for everything that uses a keyboard! – Aert Feb 27 '13 at 21:55
  • Hi Aert, I noticed that the same issue happen not only with messaging app,when I am going to insert image to my sample project which has an EditText using my custom keyboard. Therefore it may not be the issue of messaging app. But when I set the image to that sample project with code (as the sample you suggested), it works well. I guess the issue happens because in SoftKeyboard, I append the spannable object to a String Builder object. But when we set it to EditText using setText method, it works well. Do you have any idea that how to insert image object to StringBuilder? Thanks in Advance..!! – harsh Feb 28 '13 at 02:45