Need to wait until media.capturePhoto is done saving the file

Hey there, I need to know if there is a good way to wait until the image is saved to the destination.

I’m using the following

media.capturePhoto({listener=captured, destination = {baseDir = system.TemporaryDirectory, filename = "file.jpg", type = "image"}})

Then, in the captured function I am trying to send the image as an smtp attachment, but the image hasn’t saved yet before the code is called so I end up with a file-doesn’t-exist exception.

I tried placing code after media.capturePhoto as well, but it seems that this code is called almost immediately after and also doesn’t give the system time to save the image to disk.

How can I wait until the image is saved by the media.capturePhoto function?

Is there an event listener?

My options I can think of right now:

I just hack a workaround and wait 5 seconds?

I manually save the image by not including the destination table, allowing the captured function to receive the image as a display object, saving that display object to disk and finally passing that file to smtp.

Hi Daniel.

Yes, there is a listener function and you actually have it written already on your code above!

My apologies, turns out I hadn’t set the correct base directory when doing an io.path in the SMTP function. It was trying to look for the file in the documents directory when I had saved it to the temporary directory.

Also for those of you doing something similar, don’t forget to adjust the timeout to a higher value to give the function time to upload the image.

If you’re using the native mail popup, this stuff isn’t an issue, but I wanted to bypass user interaction in this step, hence all the fuss.

Daniel,

I think you are referring to the timeout on network.request, right?  The timeout is only when you don’t get any answer from the other side, so the app give up waiting. During upload, there is a constant exchange of data, so the timeout shouldn’t be a problem.

I’m talking about require(“socket.smtp”).send() function.

For some reason, I kept getting timeout exception messages when attaching large files (1MB or more). I added timeout = 60 to the options table and that had solved it.

Hi Daniel.

Yes, there is a listener function and you actually have it written already on your code above!

My apologies, turns out I hadn’t set the correct base directory when doing an io.path in the SMTP function. It was trying to look for the file in the documents directory when I had saved it to the temporary directory.

Also for those of you doing something similar, don’t forget to adjust the timeout to a higher value to give the function time to upload the image.

If you’re using the native mail popup, this stuff isn’t an issue, but I wanted to bypass user interaction in this step, hence all the fuss.

Daniel,

I think you are referring to the timeout on network.request, right?  The timeout is only when you don’t get any answer from the other side, so the app give up waiting. During upload, there is a constant exchange of data, so the timeout shouldn’t be a problem.

I’m talking about require(“socket.smtp”).send() function.

For some reason, I kept getting timeout exception messages when attaching large files (1MB or more). I added timeout = 60 to the options table and that had solved it.