Android:可以将文件附加到电子邮件而不写入 SD 吗?
我的应用程序将数据本地存储在本机 SQLite 数据库中,我希望允许用户通过向自己发送电子邮件 .csv 文件来导出此数据。为了做到这一点,我从数据库生成 .csv 并将其写入 SD 卡,然后将其附加到电子邮件:
StringBuilder csv = generateFile();
writeFile(csv.toString(),"file.csv");
Intent email = new Intent(android.content.Intent.ACTION_SEND);
email.setType("application/octet-stream");
email.putExtra(android.content.Intent.EXTRA_STREAM, Uri.parse("file://sdcard/file.csv"));
这一切都很好。不过,我想知道是否可以跳过先写入SD的步骤,直接附加数据。
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.putExtra(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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
即使有可能,我也建议不要这样做。
用于启动活动的意图将被保留(可能)相当长的时间——只要相关活动是“活动的”并且可以想象返回(例如,回到堆栈上) ,因为用户在撰写电子邮件时接了电话,然后通过短信聊天了半个小时)。
此外,作为此过程的一部分,
Intents
在进程之间进行了相当多的复制。例如,电子邮件客户端将处于与您的应用程序不同的进程中。由于这两个原因,您需要保持
Intents
较小。内容的Uri
的唯一替代方案是将内容直接放在 extra 本身中......并且该 CSV 文件可能会变得有点大。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 aUri
to the content would be to have the content directly in the extra itself...and that CSV file presumably could get kinda big.