Android:可以将文件附加到电子邮件而不写入 SD 吗?

发布于 2024-08-13 05:04:14 字数 454 浏览 2 评论 0原文

我的应用程序将数据本地存储在本机 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心如狂蝶 2024-08-20 05:04:14

即使有可能,我也建议不要这样做。

用于启动活动的意图将被保留(可能)相当长的时间——只要相关活动是“活动的”并且可以想象返回(例如,回到堆栈上) ,因为用户在撰写电子邮件时接了电话,然后通过短信聊天了半个小时)。

此外,作为此过程的一部分,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 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文