8

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. Unfortunately, based on the lone response to the below question, it seems there is no good way to know that the email application is done with the temporary file.

Android: problem sending email with attachment from my application

Because I can't receive any notification that the email is done with the file, my rules for deleting temporary files are pretty bad. They are something like "check for temp files onPause and onCreate; delete anything over 5 minutes old".

Because my rules are so ugly, I'm particularly concerned where I should write the files. I can not write them to the internal cache directory because their size can greatly exceed 1mb. Is it reasonable to create a folder devices' sdcard: "/sdcard/myapp_tmp"? What is the common practice for this situation?

Community
  • 1
  • 1
ab11
  • 19,770
  • 42
  • 120
  • 207

1 Answers1

12

You can use Context's getExternalCacheDir() method to get a File reference where you can store files on an SD card. Of course, you'll have to do the usual checks to make sure the external storage is mounted and writable, as usual, but this is probably the best place to store that type of temporary file. One thing you might want to do is just set a maximum amount of space that you can use in the cache directory, and then, any time you need to write a new temporary file, if that file exceeds the maximum space, then start deleting the temp files, starting with the oldest, until there is sufficient space.

EDIT: Alternatively, maybe something like this would work:

if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
    File externalRoot = Environment.getExternalStorageDirectory();
    File tempDir = new File(externalRoot, ".myAppTemp");
}

Prepending the "." should make the folder hidden, I'm fairly sure.

Kevin Coppock
  • 133,643
  • 45
  • 263
  • 274
  • Thank you. Unfortunately that method is API level 8. I had hoped to support level 6 and up. – ab11 May 05 '11 at 14:39
  • Ah, so it is. I've added a new suggestion that works for API levels 1+. – Kevin Coppock May 05 '11 at 14:51
  • "." does makes the directory hidden. Perfect. Thanks. – ab11 May 05 '11 at 15:24
  • 1
    what if user dont have mounted sd card , No user keeps sd card if they have internal memory more that gbs. – nicky Aug 12 '12 at 17:17
  • 3
    even in devices with internal memory in gbs, there exists a path for external storage directory and external cache dir so dont worry about that. – Yogesh Maheshwari Nov 01 '12 at 10:50
  • looks like a good solution but what happen if say i set a max size of 10mb for my temporary cache dir and i am creating two 9mb files... which need to be sent by email... one of the file will eventually get erased – yeahman May 22 '13 at 16:31
  • I am using getExternalCacheDir to store my temporary files for sending by email... when does the system/android cleans up the cache dir? I don't want my files to be deleted while it is being sent by mail... is there a maximum cache limit? i need at most about 25mb per email – yeahman Oct 23 '13 at 16:41