Message.setAttachment()方法的javadoc说“如果存在则设置消息附件”,这意味着如果文件不存在,它将无提示失败。
我的代码中的if()语句确保除非文件存在,否则不会调用Message.setAttachment()。但电子邮件到了,所以我知道该文件存在并且setAttachment正在调用。我也知道它长15823字节,因为电子邮件告诉我这个。但没有附加任何内容。两个月前,这个工作得很好。难道我做错了什么?或者这是setAttachment()方法中的一个新bug?
String imageFile = appHomePath + baseName;
try (OutputStream os = FileSystemStorage.getInstance().openOutputStream(imageFile)) {
ImageIO.getImageIO().save(screenShot, os, ImageIO.FORMAT_PNG, 1);
}
if (FileSystemStorage.getInstance().exists(imageFile)) {
long size = FileSystemStorage.getInstance().getLength(imageFile);
Message emailMessage = new Message(imageFile + "\n\n" + size + " bytes\n" + notes);
emailMessage.setAttachment(imageFile);
// emailMessage.setAttachment("file://" + imageFile); // Didn't work either
emailMessage.setMimeType("image/png");
String[] recipients = {"SwingGuy1024@yahoo.com"};
Message.sendMessage(recipients, "Snap", emailMessage);
} else {
Dialog.show("Missing file", "Missing: "+imageFile, Dialog.TYPE_ERROR, null, "OK", null);
}
附录: 这是我得到的一对堆栈跟踪:(我应该提到我不明白为什么FileNotFoundException给出的路径不是我指定的路径。我指定的路径记录在下面的第二行,作为图像文件路径:)
D/EmailAttachmentTest(12124): [EDT] 0:0:13,592 - appHomePath: /data/data/com.mycompany.myapp/files/
D/EmailAttachmentTest(12124): [EDT] 0:0:13,614 - image file path: /data/data/com.mycompany.myapp/files/snapshot19.png
D/EmailAttachmentTest(12124): [EDT] 0:0:13,852 - Attachments: 1
D/EmailAttachmentTest(12124): [EDT] 0:0:13,853 - Attachment: /data/data/com.mycompany.myapp/files/snapshot19.png maps to image/png
W/ResourceType(12124): No package identifier when getting value for resource number 0x00000000
W/ResourceType(12124): No package identifier when getting value for resource number 0x00000000
W/ResourceType(12124): No package identifier when getting value for resource number 0x00000000
W/ResourceType(12124): No package identifier when getting value for resource number 0x00000000
W/ResourceType(12124): No package identifier when getting value for resource number 0x00000000
E/AndroidImplementation(12124): null
E/AndroidImplementation(12124): java.io.FileNotFoundException: /storage/emulated/legacy/tmp/snapshot19.png: open failed: EACCES (Permission denied)
E/AndroidImplementation(12124): at libcore.io.IoBridge.open(IoBridge.java:456)
E/AndroidImplementation(12124): at java.io.FileOutputStream.<init>(FileOutputStream.java:87)
E/AndroidImplementation(12124): at java.io.FileOutputStream.<init>(FileOutputStream.java:127)
E/AndroidImplementation(12124): at java.io.FileOutputStream.<init>(FileOutputStream.java:116)
E/AndroidImplementation(12124): at com.codename1.impl.android.AndroidImplementation.createFileOuputStream(AndroidImplementation.java:4380)
E/AndroidImplementation(12124): at com.codename1.impl.android.AndroidImplementation.openFileOutputStream(AndroidImplementation.java:4301)
E/AndroidImplementation(12124): at com.codename1.io.FileSystemStorage.openOutputStream(FileSystemStorage.java:274)
E/AndroidImplementation(12124): at com.codename1.impl.android.AndroidImplementation.fixAttachmentPath(AndroidImplementation.java:4485)
E/AndroidImplementation(12124): at com.codename1.impl.android.AndroidImplementation.sendMessage(AndroidImplementation.java:4537)
E/AndroidImplementation(12124): at com.codename1.ui.Display.sendMessage(Display.java:3252)
E/AndroidImplementation(12124): at com.codename1.messaging.Message.sendMessage(Message.java:159)
E/AndroidImplementation(12124): at com.mycompany.myapp.EmailAttachmentTest$MainForm.saveSnapshot(EmailAttachmentTest.java:249)
E/AndroidImplementation(12124): at com.mycompany.myapp.EmailAttachmentTest$MainForm.dumpScreen(EmailAttachmentTest.java:206)
E/AndroidImplementation(12124): at com.mycompany.myapp.EmailAttachmentTest$MainForm.lambda$new$0(EmailAttachmentTest.java:93)
E/AndroidImplementation(12124): at com.mycompany.myapp.EmailAttachmentTest$MainForm.access$lambda$1(EmailAttachmentTest.java)
E/AndroidImplementation(12124): at com.mycompany.myapp.EmailAttachmentTest$MainForm$$Lambda$2.actionPerformed(Unknown Source)
E/AndroidImplementation(12124): at com.codename1.ui.util.EventDispatcher.fireActionEvent(EventDispatcher.java:349)
E/AndroidImplementation(12124): at com.codename1.ui.Button.fireActionEvent(Button.java:411)
E/AndroidImplementation(12124): at com.codename1.ui.Button.released(Button.java:442)
E/AndroidImplementation(12124): at com.codename1.ui.Button.pointerReleased(Button.java:530)
E/AndroidImplementation(12124): at com.codename1.ui.Form.pointerReleased(Form.java:2620)
E/AndroidImplementation(12124): at com.codename1.ui.Form.pointerReleased(Form.java:2556)
E/AndroidImplementation(12124): at com.codename1.ui.Component.pointerReleased(Component.java:3147)
E/AndroidImplementation(12124): at com.codename1.ui.Display.handleEvent(Display.java:2024)
E/AndroidImplementation(12124): at com.codename1.ui.Display.edtLoopImpl(Display.java:1066)
E/AndroidImplementation(12124): at com.codename1.ui.Display.mainEDTLoop(Display.java:995)
E/AndroidImplementation(12124): at com.codename1.ui.RunnableWrapper.run(RunnableWrapper.java:120)
E/AndroidImplementation(12124): at com.codename1.impl.CodenameOneThread$1.run(CodenameOneThread.java:60)
E/AndroidImplementation(12124): at java.lang.Thread.run(Thread.java:818)
E/AndroidImplementation(12124): Caused by: android.system.ErrnoException: open failed: EACCES (Permission denied)
E/AndroidImplementation(12124): at libcore.io.Posix.open(Native Method)
E/AndroidImplementation(12124): at libcore.io.BlockGuardOs.open(BlockGuardOs.java:186)
E/AndroidImplementation(12124): at libcore.io.IoBridge.open(IoBridge.java:442)
E/AndroidImplementation(12124): ... 28 more
W/Bundle (12124): Key android.intent.extra.TEXT expected ArrayList<CharSequence> but value was a java.lang.String. The default value <null> was returned.
W/Bundle (12124): Attempt to cast generated internal exception:
W/Bundle (12124): java.lang.ClassCastException: java.lang.String cannot be cast to java.util.ArrayList
W/Bundle (12124): at android.os.BaseBundle.getCharSequenceArrayList(BaseBundle.java:1060)
W/Bundle (12124): at android.os.Bundle.getCharSequenceArrayList(Bundle.java:884)
W/Bundle (12124): at android.content.Intent.getCharSequenceArrayListExtra(Intent.java:5182)
W/Bundle (12124): at android.content.Intent.migrateExtraStreamToClipData(Intent.java:7865)
W/Bundle (12124): at android.content.Intent.migrateExtraStreamToClipData(Intent.java:7828)
W/Bundle (12124): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1561)
W/Bundle (12124): at android.app.Activity.startActivityForResult(Activity.java:3755)
W/Bundle (12124): at android.app.Activity.startActivityForResult(Activity.java:3716)
W/Bundle (12124): at com.codename1.impl.android.CodenameOneActivity.startActivityForResult(CodenameOneActivity.java:541)
W/Bundle (12124): at com.codename1.impl.android.AndroidNativeUtil.startActivityForResult(AndroidNativeUtil.java:140)
W/Bundle (12124): at com.codename1.impl.android.AndroidImplementation.sendMessage(AndroidImplementation.java:4566)
W/Bundle (12124): at com.codename1.ui.Display.sendMessage(Display.java:3252)
W/Bundle (12124): at com.codename1.messaging.Message.sendMessage(Message.java:159)
W/Bundle (12124): at com.mycompany.myapp.EmailAttachmentTest$MainForm.saveSnapshot(EmailAttachmentTest.java:249)
W/Bundle (12124): at com.mycompany.myapp.EmailAttachmentTest$MainForm.dumpScreen(EmailAttachmentTest.java:206)
W/Bundle (12124): at com.mycompany.myapp.EmailAttachmentTest$MainForm.lambda$new$0(EmailAttachmentTest.java:93)
W/Bundle (12124): at com.mycompany.myapp.EmailAttachmentTest$MainForm.access$lambda$1(EmailAttachmentTest.java)
W/Bundle (12124): at com.mycompany.myapp.EmailAttachmentTest$MainForm$$Lambda$2.actionPerformed(Unknown Source)
W/Bundle (12124): at com.codename1.ui.util.EventDispatcher.fireActionEvent(EventDispatcher.java:349)
W/Bundle (12124): at com.codename1.ui.Button.fireActionEvent(Button.java:411)
W/Bundle (12124): at com.codename1.ui.Button.released(Button.java:442)
W/Bundle (12124): at com.codename1.ui.Button.pointerReleased(Button.java:530)
W/Bundle (12124): at com.codename1.ui.Form.pointerReleased(Form.java:2620)
W/Bundle (12124): at com.codename1.ui.Form.pointerReleased(Form.java:2556)
W/Bundle (12124): at com.codename1.ui.Component.pointerReleased(Component.java:3147)
W/Bundle (12124): at com.codename1.ui.Display.handleEvent(Display.java:2024)
W/Bundle (12124): at com.codename1.ui.Display.edtLoopImpl(Display.java:1066)
W/Bundle (12124): at com.codename1.ui.Display.mainEDTLoop(Display.java:995)
W/Bundle (12124): at com.codename1.ui.RunnableWrapper.run(RunnableWrapper.java:120)
W/Bundle (12124): at com.codename1.impl.CodenameOneThread$1.run(CodenameOneThread.java:60)
W/Bundle (12124): at java.lang.Thread.run(Thread.java:818)
答案 0 :(得分:0)
在哪个操作系统上?
如果这是在Android上,那么它对文件的位置非常敏感,我建议总是把它放在实际的应用程序主目录中而不是其他地方。
由于我们产生了一个发送消息的外部进程,我们并不知道出了什么问题,因为在某些操作系统中没有来自进程的错误响应。