【问题标题】:How to convert picture into text and send it as a text message in android如何将图片转换为文本并在android中作为短信发送
【发布时间】:2016-04-01 16:41:05
【问题描述】:

我开发了一个安卓应用,用户可以使用 SmsManager Api 向任意号码发送消息。

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

现在我希望用户使用

将小图片发送到任意号码
smsManager.sendTextMessage("phoneNo", null, picture, null, null);

我不想通过彩信发送这张图片。我知道这可以通过在发送端将图片转换为字符串并在接收端将字符串重新转换为图片来实现。但我不知道如何做到这一点。这是已完成此任务的 android 应用程序的快照。我想这样做,如快照链接所示。 Here is snapshot

【问题讨论】:

  • 您不能发送文本格式的图片。 SMS 不允许发送除字符以外的任何内容。
  • 不,我的意思是 png 中的贴纸,如 viber、imo、telegram 中的贴纸

标签: android


【解决方案1】:

如果您的应用具有发送/接收 SMS 功能,为了能够通过 SMS 发送,我建议您将位图转换为 Base64 字符串。这是一个示例代码:

    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;
        }
     }

【讨论】:

  • 这种通过短信发送图像的方法有多可行?您可以推荐吗?
【解决方案2】:

首先将Bitmap转换为字节数组

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

然后将其转换为字符串

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

在另一端反转过程

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

最后

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

【讨论】:

    【解决方案3】:

    您可以使用此库转换任何图像。

    https://github.com/w446108264/XhsEmoticonsKeyboard

    【讨论】:

      猜你喜欢
      • 2016-07-07
      • 1970-01-01
      • 2012-03-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多