开发者

Sharing Temporary Files Between Apps with No SD Card

开发者 https://www.devze.com 2023-03-07 16:41 出处:网络
If I create a temp file in my application\'s cache di开发者_如何学运维rectory I can\'t, for instance, email it as an attachment. This is because the other app doesn\'t have permission to read the file

If I create a temp file in my application's cache di开发者_如何学运维rectory 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?


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)

0

精彩评论

暂无评论...
验证码 换一张
取 消