安卓:可以将文件附加到电子邮件,而无需编写到SD?写到、电子邮件、文件、SD

2023-09-06 13:35:03 作者:你的幸福,与我无关

我的应用程序将数据存储在本地,在本地的SQLite数据库,我想允许用户通过电子邮件自理.csv文件导出此数据。为了做到这一点,我产生从数据库中.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"));

所有的伟大工程。我想知道,虽然,是如果它是可以跳过写入到SD第一的步骤,并直接连接的数据

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.

对于这两个原因,你需要保持你的意图小。唯一的选择对乌里的内容将直接有内容的额外的本身......这CSV文件presumably可以得到还挺大的。

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.