6

My application allows users to create and modify files. I would like them to be able to send a file as an email attachment. So, I need to first create and write to a temporary file, which I then attach to the email. And then I would like to delete the temporary file when the email program finishes. Unfortunately, the gmail app responds with a result code as soon as the user clicks "send"; and if I delete the file as soon as the result code is received, no attachment is sent.

Its possible that something else is going wrong and the attachment is not sent for a different reason, but I'm pretty sure my assessment is correct because the below code works properly if I comment out the mEmailTmpFile.delete() call. It also works fine if I do something very undesirable like Thread.sleep(4000) immediately prior to mEmailTmpFile.delete().

Is there anyway to be notified when the email is done sending? Or any other suggestions for how I should work around this?

//send an email...

File externalStorage = Environment.getExternalStorageDirectory();
String sdcardPath = externalStorage.getAbsolutePath();
mEmailTmpFile = new File(sdcardPath + "/" +  name );

//do some other to ensure unqiueness and then write to the file...

//all done writing, send email

Intent sendIntent = new Intent(Intent.ACTION_SEND);
sendIntent.setType("application/zip");
sendIntent.putExtra(Intent.EXTRA_SUBJECT, name);
sendIntent.putExtra(Intent.EXTRA_TEXT, "File attached.");
sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://"+ mEmailTmpFile.getPath()));
startActivityForResult(Intent.createChooser(sendIntent, "Email"), REQUESTCODE_EMAIL);

public synchronized void onActivityResult(int reqCode, int resultCode, Intent data)
{
   if (reqCode == REQUESTCODE_EMAIL)
   {
    mEmailTmpFile.delete();
    }
}
ab11
  • 19,770
  • 42
  • 120
  • 207
  • Not according to [this answer](http://stackoverflow.com/questions/5471217/trivial-get-confirmation-of-email-sent-in-android/5471463#5471463). – bzlm May 02 '11 at 17:06

1 Answers1

0

In my apps, I don't delete the temporary file. Android will take care of it by deleting the file if it needs space. I would ensure that you don't create the tmp file in the SDCard root directory since that can look messy but other than that there shouldn't be a problem.

WanderingPhd
  • 189
  • 1
  • 9
  • 4
    Android will not delete the file unless you set File.deleteOnExit(), however I have found this to be unreliable. Also you may be referring to files created in the cache directory, however, those files will not be accessible by Gmail and thus can't be used here. – Greg Ennis Jul 14 '11 at 00:37
  • @grennis Why can't email apps access files in the cache directory? Did you try world readable mode? – class stacker Jan 03 '13 at 07:47
  • Yes it won't work as of Android gingerbread, the last time I had tried it – Greg Ennis Jan 04 '13 at 15:00