Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add image to Messaging app in Android

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..!!

like image 737
harsh Avatar asked Nov 12 '22 10:11

harsh


1 Answers

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.

like image 123
Aert Avatar answered Nov 15 '22 11:11

Aert