Reputation: 4314
Trying to send a SQLite db via email. The email send successfully but there is no attachment with it. My code is :
Uri uri;
if(android.os.Build.VERSION.SDK_INT >= 4.2)
{
uri = Uri.fromFile (
new File (
getApplicationContext().getApplicationInfo().dataDir +
"/databases/"+ MainActivity.accounts.getUserName()+ "D"+"/Dentist.db"
)
);
} else {
uri = Uri.fromFile (
new File (
"/data/data/" + getApplicationContext().getPackageName() +
"/databases/"+MainActivity.accounts.getUserName()+ "D"+"/Dentist.db"
)
);
}
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("application/db");
shareIntent.putExtra(Intent.EXTRA_EMAIL,new String[] { "" });
shareIntent.putExtra(Intent.EXTRA_SUBJECT,"Test");
shareIntent.putExtra(Intent.EXTRA_TEXT, "");
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(shareIntent);
Upvotes: 1
Views: 2215
Reputation: 162
Make sure you can successfully export the file before sending it by allowing the application to write in the External Storage.
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
Upvotes: 0
Reputation: 52800
Try this code, you have to pass your db name and email address on which you want to share db file.
@SuppressWarnings("resource")
public static void exportDatabse(Context ctx) {
File backupDB = null;
try {
File sd = Environment.getExternalStorageDirectory();
File data = Environment.getDataDirectory();
if (sd.canWrite()) {
String currentDBPath = "//data//" + ctx.getPackageName()
+ "//databases//" + "Your_db_name" + "";
File currentDB = new File(data, currentDBPath);
backupDB = new File(sd, "Your_db_name");
if (currentDB.exists()) {
FileChannel src = new FileInputStream(currentDB)
.getChannel();
FileChannel dst = new FileOutputStream(backupDB)
.getChannel();
dst.transferFrom(src, 0, src.size());
src.close();
dst.close();
}
}
} catch (Exception e) {
e.printStackTrace();
}
Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.setType("*/*");
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL,
new String[] { "[email protected]" });
Random r = new Random();
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,
"Local db " + r.nextInt());
emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(backupDB));
ctx.startActivity(Intent.createChooser(emailIntent, "Export database"));
}
Done
Upvotes: 2
Reputation: 2124
The app which sends the e-mail doesn't have access to your db-file. You have to copy the db-file to your apps external-files directory before you attach it to the e-mail.
You can get the path of the external-files directory via
context.getExternalFilesDir(null)
Upvotes: 1