Email a db file Android

2.2k views Asked by At

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);
3

There are 3 answers

0
Hiren Patel On BEST ANSWER

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

0
Stefan Wanitzek On

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)
0
Boz On

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" />