Email loss ignoring line breaks in predefined text (Android)

In various Android applications, I use the following code to show the choice of an application for email, and after the user decides to select one of the applications, paste the predefined text into the email form:

final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND); emailIntent.setType("plain/text"); emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[] { " info@example.org " }); emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Sample subject"); String contentStr = ""; for (Object o : mArrayList) { // mArrayList: ArrayList<Object> content = contentStr+o.toString()+"\n"; } emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, backupStr); startActivity(Intent.createChooser(emailIntent, "Choose application")); 

In a for-loop, the output of a string of objects is concatenated with the temporary string "contentStr". After each object there should be a line break ("\ n").

Therefore, when I test this code on my phone, it works fine, and each individual object has its own line.

But users report that their email application (Android standard also) puts everything on one line and ignores line breaks.

So am I doing something wrong? Or can I just ignore this error report as it is not a problem that the developer can solve?

+6
source share
4 answers

2 potential contacts:

  • Try using \ r or \ n \ r instead of just \ n or even two line breaks.
  • Use HTML line breaks ( <br> )

Also, something is wrong with your code here

 String contentStr = ""; for (Object o : mArrayList) { // mArrayList: ArrayList<Object> content = contentStr+o.toString()+"\n"; } emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, backupStr); 

This does not put anything in the intent, contentStr is empty, and the content contains only the last object.

This is the best, shorter and more efficient method:

 contentStr = TextUtils.join("\n", mArrayList); 
+3
source

using:

 emailIntent.setType("text/plain"); 

ACTION_SEND

 final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND); emailIntent.setType("text/plain"); emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[] { " info@example.org " }); emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Sample subject"); String contentStr = ""; for (Object o : mArrayList) { // mArrayList: ArrayList<Object> content = contentStr+o.toString()+"\n"; } emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, backupStr); startActivity(Intent.createChooser(emailIntent, "Choose application")); 
0
source

I use something like the following to not get SMS / Twitter / Facebook apps there:

 Intent emailIntent = new Intent(Intent.ACTION_SEND, Uri.parse("mailto: someemail@gmail.com ")); emailIntent.setType("text/plain"); emailIntent.putExtra(Intent.EXTRA_EMAIL, new String[] {" someemail@gmail.com "}); emailIntent.putExtra(Intent.EXTRA_TEXT, msg ); emailIntent.putExtra(Intent.EXTRA_SUBJECT, "This is my Error Report"); 

And here String msg was created using StringBuilder and contains \ n for line breaks. Basically, I think you just need to change your MIME type and you will be installed.

0
source

You need to replace \n with <br/> even if you put EXTRA_TEXT . There is my code:

 final Intent intent = new Intent(Intent.ACTION_SEND_MULTIPLE); intent.setType("message/rfc822"); intent.putExtra(Intent.EXTRA_EMAIL, " xxx@xxx.com "); intent.putExtra(Intent.EXTRA_SUBJECT, "subject"); intent.putExtra(Intent.EXTRA_TEXT, "This\nis\na\ntest!".replace("\n", "<br/>");); 
0
source

Source: https://habr.com/ru/post/910751/


All Articles