开发者

Android: possible to attach a file to email without writing to SD?

开发者 https://www.devze.com 2022-12-13 05:36 出处:网络
My application stores data locally in the native SQLite db, and I want to allow users to export this data by emailing themself a .csv file. In order to do this I\'m generating the .csv from the databa

My application stores data locally in the native SQLite db, and I want to allow users to export this data by emailing themself a .csv file. In order to do this I'm generating the .csv from the database and writing it to the SD card, then attaching it to an email:

StringBuilder csv = generateFile();
writeFile(csv.toString(),"file.csv");
Intent email = new Intent(android.content.Intent.ACTION_SEND);
email.setType("application/octet-stream");
email.putEx开发者_运维百科tra(android.content.Intent.EXTRA_STREAM, Uri.parse("file://sdcard/file.csv"));

Which all works great. What I'm wondering, though, is if it is possible to skip the step of writing to SD first, and directly attach the data.


Even if it is possible, I recommend against it.

Intents used to launch activities will be held onto for (potentially) a fairly long time -- as long as the activity in question is "alive" and could conceivably be returned to (e.g., back on the stack, because the user took a phone call while composing the email, then chatted via SMS for a half-hour).

Moreover, Intents get copied between processes a fair bit as part of this. For example, the email client will be in a different process than your app.

For both of these reasons, you need to keep your Intents small. The only alternative to a Uri to the content would be to have the content directly in the extra itself...and that CSV file presumably could get kinda big.

0

精彩评论

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