Atul Yadav
Atul Yadav

Reputation: 2040

Send SQLite Database as email

In my app I want to send the history stored in my SQLite databse via email to another person, (i.e as there is an option in 'Whatsapp' to send user chat via email, as a text file).

So here I just want an approach how to begin with this problem.

Thanks

Upvotes: 1

Views: 4999

Answers (1)

Simon Dorociak
Simon Dorociak

Reputation: 33515

You need to add EXTRA_STREAM to your Intent that sends e-mail:

intent.setType("application/octet-stream");
intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(databaseFile));

Where databaseFile is your database file.

Don't forget that you need to copy your file on external storage because you cannot attach file(s) stored in application-package directory.

Whole code can looks like:

Intent i = new Intent(Intent.ACTION_SEND);
i.putExtra(Intent.EXTRA_EMAIL, new String[] {"[email protected]"});
i.putExtra(Intent.EXTRA_SUBJECT, "Subject");
i.putExtra(Intent.EXTRA_TEXT, "Some message text");
i.setType("application/octet-stream");
i.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(databaseFile));
startActivity(Intent.createChooser(i, "Send e-mail"));

If you want to put multiple attachements, you can use:

Intent i = new Intent(Intent.ACTION_SEND_MULTIPLE);
...
ArrayList<Uri> items = new ArrayList<Uri>();
Uri uri = Uri.fromFile(new File(pathToFile));       
items.add(uri);
i.putParcelableArrayListExtra(Intent.EXTRA_STREAM, items);

Note: Same will work for txt files.

Upvotes: 3

Related Questions