Android email replaces space with plus(+)

早过忘川 提交于 2019-12-11 05:36:34

问题


I am sending an email in action view, it works perfectly fine in gmail , but if the user chooses any other mailing service it replaces spaces with '+'

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

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

Any idea how to solve this issues

Here is my function for sending email

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

回答1:


Try this code.

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:


From the description of the method URLEncoder.encode

java.net.URLEncoder.encode(String s) Deprecated. use encode(String, String) instead. Encodes a given string s in a x-www-form-urlencoded string using the specified encoding scheme enc.

All characters except letters ('a'..'z', 'A'..'Z') and numbers ('0'..'9') and characters '.', '-', '*', '_' are converted into their hexadecimal value prepended by '%'. For example: '#' -> %23. In addition, spaces are substituted by '+'




回答3:


Use Uri.encode(String) instead of the URLEncoder, it handles spaces correctly for this use case. ACTION_VIEW with mailto link is more preferable if you wish to limit the sending options to email only.




回答4:


Just use without any encode.

"&body=" + body;

it works for me!



来源:https://stackoverflow.com/questions/12172338/android-email-replaces-space-with-plus

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!