在我的应用中,我想通过电子邮件发送日志文件内容以进行调试。但是,在Android设备上,换行符(\ n)在邮件中显示为空格,而不是实际的换行符。 (它可以在iPhone和模拟器上正常工作。)是否可以解决此问题或解决方法?
顺便说一下,模拟器为什么截断邮件文本?
编辑:我刚刚在较新的平板电脑上进行了测试(2019年相对于2013年,问题消失了。
编辑2:实际上,这取决于电子邮件客户端。测试用例:创建一个Hello world项目,并将start()更改为:
public void start() {
if(current != null){
current.show();
return;
}
Form hi = new Form("Test log", BoxLayout.y());
Button b = new Button("Send log");
b.addActionListener((ActionListener) (ActionEvent evt) -> {
String text;
try {
byte[] read = com.codename1.io.Util.readInputStream(Storage.getInstance().createInputStream("CN1Log__$"));
text = new String(read);
} catch (IOException ex) {
text = ex.getMessage();
}
sendMessage("Log file", new Message(text), "");
});
hi.add(b);
log("line 1");
log("line 2");
hi.show();
}
在Samsung Galaxy Tab A 10.1上进行测试,得到:
1)使用(三星)电子邮件客户端:
[EDT] 0:0:0,33 - Codename One revisions: 7dd4e7d08b3442d90959477ee52a5ae8c4361b29
[EDT] 0:0:0,36 - line 1
[EDT] 0:0:0,39 - line 2
2)使用(Google)Gmail客户端:
[EDT] 0:0:0,33 - Codename One revisions: 7dd4e7d08b3442d90959477ee52a5ae8c4361b29 [EDT] 0:0:0,36 - line 1 [EDT] 0:0:0,39 - line 2
这是Windows和Unix换行行为之间的区别。 Android unix使用普通\n
,而Windows使用\r\n
,如此处所述:https://www.baeldung.com/java-string-newline