user444707
user444707

Reputation:

Android email SQLite database

I am hoping to be able to email the SQLite database I use within my app as a form of backup that the user can perform. My current code is below, the database shows up as an attachment in the email intent and the email will send, but the attachment is not sent.

File file = new File(Environment.getDataDirectory(), "/data/com.app/databases/databaseName");
Intent intent = new Intent(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_EMAIL, new String[]{""});
intent.putExtra(Intent.EXTRA_SUBJECT, "Backup");
intent.putExtra(Intent.EXTRA_TEXT, "");
intent.setType("application/octet-stream");
intent.putExtra(Intent.EXTRA_STREAM, file.toURI());
startActivity(Intent.createChooser(intent, "Send Email"));

Upvotes: 8

Views: 5236

Answers (4)

Mike
Mike

Reputation: 1

watch this link : http://www.openintents.org/en/node/121 you need passed URI

the solution is:

files have to be attached as

sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///sdcard/mysong.mp3"));

You need the extra / in file:/// (missing in the Stack Overflow post) as otherwise GMail drops the attachment just before sending stating the file path is wrong.

SO YOU NEED TH EXTRA / in file and its not droped.... hope this helps

Upvotes: 0

Joshua Pinter
Joshua Pinter

Reputation: 47551

https://stackoverflow.com/a/18548685/293280

The above SO Answer does a great job at showing you how to copy the DB file, email it as an attachment, and then delete the copied file. Check it out.

Upvotes: 0

Export your database to the sd card before trying to email it. You can't add attachments from within the data/data folder of your application. Those files are private to your application.

Upvotes: 4

dfetter88
dfetter88

Reputation: 5391

I remember I had this exact problem some time back. I'm not sure what I did to fix it, but my working code looks something like this...

File root = Environment.getExternalStorageDirectory();
String fileName = "foo.txt";
  if (root.canWrite()) {
    attachment = new File(root, fileName);
  }
email.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(attachment));
startActivity(Intent.createChooser(email, "Send Email"));

I do remember that since I was using the SD card for storage, it would not send my attachment if I was still plugged into my computer via USB (since it kept the SD card mounted and busy). Once I unplugged the USB connection, things worked well.

Upvotes: 3

Related Questions