Sending an e-mail message with JSON text attachment from a temporary file

67 views
Skip to first unread message

P5music

unread,
Dec 14, 2020, 6:25:33 AM12/14/20
to CodenameOne Discussions
I would like to know if this code snippet will work on iOS.
I want to use the message send feature to export some application data.
The user is asked for the filename, then a temporary file is created containing the JSON text to be exported (with a custom file extension), then that file is used to create an output stream and attached like plain text (or should the json mimetype be used?), with no subject and no recipients.
Thanks in advance

  try {
                    file=File.createTempFile(fileName,"ext");

                    OutputStream os = Storage.getInstance().createOutputStream(file.getAbsolutePath());
                    os.write(text.getBytes("UTF-8"));
                    os.flush();
                    os.close();
                } catch (IOException e) {
                    return;
                }
                Message m = new Message("");
                m.getAttachments().put(file.getAbsolutePath(), "text/plain");
                Display.getInstance().sendMessage(new String[] {""}, "", m);

Shai Almog

unread,
Dec 14, 2020, 11:16:12 PM12/14/20
to CodenameOne Discussions
It should. But the only way to make 100% sure is to test on the device.

P5music

unread,
Dec 15, 2020, 3:41:21 AM12/15/20
to CodenameOne Discussions
Excuse me, I have another question about this.
The app has to check if the file is already present. It could happen when the user types the same file name before the temp directory has been emptied.
So I have to check it and delete before creating it again.
But I wonder if temp files are special files, how to check it without any uncertainty?
Thanks

Shai Almog

unread,
Dec 15, 2020, 10:45:37 PM12/15/20
to CodenameOne Discussions
I don't follow?
What's the problem with exists() ?

P5music

unread,
Dec 16, 2020, 5:52:09 AM12/16/20
to CodenameOne Discussions
Maybe what I am saying does not make sense, but if I create the temp file I am already going to get an error if the file exists.
So I should use
file=new File(fileName+".ext");

and then create it with
if (!file.exists()) file=File.createTempFile(fileName,"ext");
?
Regards 

Shai Almog

unread,
Dec 16, 2020, 10:35:55 PM12/16/20
to CodenameOne Discussions
You need to save a reference to the created temp file to check if it exists as the name is random.

P5music

unread,
Dec 17, 2020, 4:06:00 AM12/17/20
to CodenameOne Discussions

I want to know whether the creation of a temp file with that method will give an error if the file already exists with that file name.
I think that the file name is not random because it is a parameter of the createTempFile method, is it?

Shai Almog

unread,
Dec 17, 2020, 11:53:46 PM12/17/20
to CodenameOne Discussions
No. That's not a file name. That's a prefix and suffix. The actual name would be longer.

P5music

unread,
Dec 18, 2020, 3:47:40 AM12/18/20
to CodenameOne Discussions
Ok
Can the temporary file be renamed with the entered filename? This is for the user to see and attach a file with a meaningful name.
Thanks

Shai Almog

unread,
Dec 19, 2020, 12:54:54 AM12/19/20
to CodenameOne Discussions
Yes but then you might as well just create a non-temporary file with a specific name and clear it when the app exits or periodically. You can create a folder to store these files and wipe it occasionally.

P5music

unread,
Dec 19, 2020, 10:20:09 AM12/19/20
to CodenameOne Discussions
Is this going to be working as described?
....
....
try {
file=File.createTempFile("","");
String path=file.getAbsolutePath().substring(0,file.getAbsolutePath().lastIndexOf("/"));
File oldFile=new File(path+"/"+fileName+".ext");
if (oldFile.exists()) {
oldFile.delete();
file=File.createTempFile("","");
}

file.renameTo(oldFile);

OutputStream os = Storage.getInstance().createOutputStream(file.getAbsolutePath());
....
....

Shai Almog

unread,
Dec 19, 2020, 8:58:03 PM12/19/20
to CodenameOne Discussions
There's a minimum length to the temp file prefix/sufix. As far as I recall you can't create the file with blank prefix/suffix but I might be wrong.

It very likely won't work and I would avoid mixing temp files and regular files. Temp files can reside on a different volume and renaming them to a regular file might not work.

P5music

unread,
Dec 20, 2020, 9:22:54 AM12/20/20
to CodenameOne Discussions
I see. I would like to know what form has the real name of the temp file, if you have seen it sometimes.
I could use this instruction:
file=File.createTempFile(fileName"+"_",".ext");
to have something resembling a user-created file name. It will have a strange string in the middle I suppose, like:
myfilename_vjh4v444vjhv4j45445h4j5.ext
Is it right?
Regards

Shai Almog

unread,
Dec 20, 2020, 9:09:03 PM12/20/20
to CodenameOne Discussions
You will have a strange long string usually with a long number in the middle.

P5music

unread,
Dec 21, 2020, 1:46:20 PM12/21/20
to CodenameOne Discussions
So I have to manage temp files inside the app.
Otherwise,
is there any chance that the sendMessage method returns after having effectively transferred the file
instead of being that the email app just shows it as an attachment (to be loaded after the user hits the send button),
so
it can be deleted immediately after the method returns?

Or is there any way to understand that the user has completed that operation? (Or just it leaved the email app back to my app)
Maybe the onStart method is called? Isn't it?
I mean, even it the email app would load the file when the user hits the "send" button, I can assume that if the user comes back to the app, the temp file can be deleted.

Thanks in advance

Shai Almog

unread,
Dec 21, 2020, 9:51:46 PM12/21/20
to CodenameOne Discussions
This is done by an external application so your app will be suspended while this occurs. It's not ideal to rely on that but when start() is invoked again it's possible that the user finished sending. This isn't ideal because it's possible the user will momentarily return to your app to check something then go back to the compose window.
Ultimately we don't know what happened in a 3rd party app.

P5music

unread,
Dec 28, 2020, 4:28:17 AM12/28/20
to CodenameOne Discussions
I think the best thing would be deleting the temp files only when the user wants to export again, the next time.
In fact the only temp file remains in the temp folder in the meanwhile and this causes no harm.
I put a new temp package in my project but I see that the Storage seems not to allow me to list the files in the temp folder.
So how can the file in the temp folder be deleted? I would prefer not to have a workaround, for example storing the file name, and it is good to know whether the Storage can do what I am asking about in general.
Thanks in advance

Shai Almog

unread,
Dec 28, 2020, 10:10:21 PM12/28/20
to CodenameOne Discussions
Packages != Storage != File System

The files in your project don't appear on the final app except for the root files which aren't really accessible to storage or file system storage.

Storage is used for internal stuff mostly.

Attachments use FileSystemStorage which is a bit more complex and includes directories/hierarchies. Create temp under the app home directory.
Reply all
Reply to author
Forward
0 new messages