【问题标题】:Android email replaces space with plus(+)Android 电子邮件用加号(+)替换空格
【发布时间】:2012-08-29 06:53:07
【问题描述】:

我在操作视图中发送电子邮件,它在 gmail 中运行良好,但如果用户选择任何其他邮件服务,它会用“+”替换空格

like in body text is "check out it is a good day"

it displays as "check+out+it+is+a+good+day"

知道如何解决这个问题

这是我发送电子邮件的功能

private void sendToAFriend() {

    String subject = "it is a good day ";
    String body = "Check out it is a good day";

    String uriText =
        "mailto:" + 
        "?subject=" + URLEncoder.encode(subject) + 
        "&body=" + URLEncoder.encode(body);

    Uri uri = Uri.parse(uriText);

    Intent sendIntent = new Intent(Intent.ACTION_SENDTO);
    sendIntent.setData(uri);
    startActivity(Intent.createChooser(sendIntent, "Send email")); 
}

【问题讨论】:

    标签: android gmail yahoo-mail


    【解决方案1】:

    试试这个代码。

    Intent intent = new Intent(Intent.ACTION_SENDTO); // it's not ACTION_SEND
    intent.setType("text/plain");
    intent.putExtra(Intent.EXTRA_SUBJECT, "Subject of email");
    intent.putExtra(Intent.EXTRA_TEXT, "Body of email");
    intent.setData(Uri.parse("mailto:default@recipient.com")); // or just "mailto:" for blank
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // this will make such that when user returns to your app, your app is displayed, instead of the email app.
    startActivity(intent);
    

    【讨论】:

      【解决方案2】:

      来自方法 URLEncoder.encode 的描述

      java.net.URLEncoder.encode(String s) 已弃用。改用编码(字符串,字符串)。 使用指定的编码方案 enc 对 x-www-form-urlencoded 字符串中的给定字符串 s 进行编码。

      除字母 ('a'..'z', 'A'..'Z') 和数字 ('0'..'9') 和字符 '.', '-', '* 之外的所有字符', '_' 被转换成它们的十六进制值,前面加上 '%'。例如:'#' -> %23。 另外,空格用'+'代替

      【讨论】:

        【解决方案3】:

        使用Uri.encode(String) 代替 URLEncoder,它可以正确处理此用例的空格。 如果您希望将发送选项限制为仅限电子邮件,则更可取的是带有 mailto 链接的 ACTION_VIEW。

        【讨论】:

          【解决方案4】:

          不带任何编码直接使用。

          "&body=" + 正文;

          它对我有用!

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2012-10-15
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2020-05-31
            • 2018-10-21
            • 2019-06-23
            • 1970-01-01
            相关资源
            最近更新 更多