The javadocs for the Message.setAttachment() method say "Sets the message attachment if exists", which means it will silently fail if the file doesn't exist.
The if() statement in my code ensures that Message.setAttachment() won't get called unless the file exists. But the email arrives, so I know the file exists and setAttachment is getting called. I also know that it's 15823 bytes long, because the email tells me this. But nothing has been attached. Two months ago, this worked fine. Am I doing something wrong? Or is this a fresh bug in the setAttachment() method?
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);
}
Addendum: Here's a pair of stack traces that I'm getting: (I should mention that I don't understand why the FileNotFoundException gives a path that's not what I specified. The path I specified is logged in the second line below, as image file path:)
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)