Usman Shabir
Usman Shabir

Reputation: 1

How to convert picture into text and send it as a text message in android

I have develop an android app in which user can send message to any number using SmsManager Api.

SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage("phoneNo", null, "sms message", null, null);

Now I want that user will send small picture to any number using

smsManager.sendTextMessage("phoneNo", null, picture, null, null);

I don't want to send this picture through MMS.I know this can achieved by converting picture into string at sending end and reconverting string into picture at receiving end. But i don't how to do this. Here is a snapshot of android app which has acheived this task. I want to do this as shown in snapshot link. Here is snapshot

Upvotes: 0

Views: 2242

Answers (3)

fmpsagara
fmpsagara

Reputation: 485

If your app has send/receive SMS functionality, for you to be able to send thru SMS, I would suggest you to convert the bitmap into a Base64 string. Here's a sample code:

    public String BitMapToString(Bitmap bitmap){
        ByteArrayOutputStream bytes = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, bytes);
        byte[] imageBytes = bytes.toByteArray();
        String encodedImage = Base64.encodeToString(imageBytes, Base64.DEFAULT);
    return encodedImage;
    }

    public Bitmap StringToBitMap(String encodedString){
        try {
          byte [] encodeByte=Base64.decode(encodedString,Base64.DEFAULT);
          Bitmap bitmap=BitmapFactory.decodeByteArray(encodeByte, 0, encodeByte.length);
          return bitmap;
        } catch(Exception e) {
          e.getMessage();
          return null;
        }
     }

Upvotes: 4

tiny sunlight
tiny sunlight

Reputation: 6251

You can convert any image with this library.

https://github.com/w446108264/XhsEmoticonsKeyboard

Upvotes: 0

F43nd1r
F43nd1r

Reputation: 7749

First convert the Bitmap to a byte array

ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();

Then convert that to a String

String string = new String(byteArray, "UTF-8");

At the other end reverse the process

byte[] byteArray = string.getBytes("UTF-8");

and finally

Bitmap bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);

Upvotes: 1

Related Questions