Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sharing Temporary Files Between Apps with No SD Card

If I create a temp file in my application's cache directory I can't, for instance, email it as an attachment. This is because the other app doesn't have permission to read the file.

In API 9 and up, I can set the file to be world readable with setReadable(true, false). unfortunately, I am targetting API level 7+. I don't want to rely on the presence of an SD card, so I can't use the location returned by getExternalStorageDir() to store it in case no card is present.

How can I generate and share a temp file in a way that will work, even with no SD card present?

like image 957
Captain Blammo Avatar asked May 20 '11 01:05

Captain Blammo


1 Answers

final String FILENAME = "file.tmp";
final String someData = "File data";

FileOutputStream fos = openFileOutput(FILENAME, 
                                      Context.MODE_WORLD_WRITEABLE | 
                                      Context.MODE_WORLD_READABLE);
fos.write(someData.getBytes());
fos.close();

http://developer.android.com/reference/android/content/Context.html#openFileOutput(java.lang.String,%20int)

like image 179
Sam Avatar answered Oct 04 '22 02:10

Sam