从应用程序发送电子邮件数据库备份文件

时间:2014-08-02 11:59:00

标签: android database email backup

我已经为我在我的应用程序中使用的SQLite数据库创建了备份文件。我想要的只是通过电子邮件发送此备份文件。我已经实现了发送Intent的文件,但是当它们打开时,它说,你只能发送像(图像,粗略位置等)的文件。

String pathname = Environment.getExternalStorageDirectory().getAbsolutePath();
String filename = "/Android/data/<package-name>/databases/hello.db";
File file=new File(pathname, filename);
Intent i = new Intent(Intent.ACTION_SEND);
i.putExtra(Intent.EXTRA_SUBJECT, "Database Backup");
i.putExtra(Intent.EXTRA_TEXT, "Hey there, database successfully sent.");
i.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));`i.setType("text/plain");
startActivity(Intent.createChooser(i, "Your email id"));

2 个答案:

答案 0 :(得分:0)

问题是由于安全原因,您无法通过电子邮件将数据从数据文件夹发送到SO,然后通过电子邮件将其导出到SD卡然后发送,它应该可以正常工作。

快乐编码!!!

答案 1 :(得分:0)

将text / plain设置为附件类型可能会导致电子邮件客户端尝试将文件预览为文本。您应该使用其他MIME类型,例如&#34; application / octet-stream&#34;,甚至&#34;任何&#34;面具 - &#34; * / *&#34;。

这是一个非常脏的简单片段,对我来说很好用:

File dbFile = new File(Environment.getExternalStorageDirectory(), "example.db");
SQLiteDatabase.openOrCreateDatabase(dbFile, null);
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(Intent.EXTRA_TEXT, "example text");
shareIntent.putExtra(Intent.EXTRA_SUBJECT, "database sending example");
shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(dbFile));
startActivity(shareIntent);