android - 通过电子邮件发送 db 文件 Android

标签 android email attachment

尝试通过电子邮件发送 SQLite 数据库。邮件发送成功,但没有附件。我的代码是:

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

最佳答案

试试这个代码,你必须传递你的数据库名称和你想要共享数据库文件的电子邮件地址

@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[] { "to_your_email_address@gmail.com" });

        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"));
    }

完成

关于android - 通过电子邮件发送 db 文件 Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30728011/

相关文章:

PHP mail() 附件问题

android - onTextChanged() 在 EditText 中无法正常工作

android - 在运行时绘制 HSV 圆

java - 如何解析 android Activity_main.xml 文件并修改它?

php - 如何将 PHP 邮件程序发送到通过 MySQL 查询收到的多个电子邮件地址

css - Gmail 忽略 HTML 电子邮件表格宽度和样式

mapping - Elasticsearch 如何在ouchdb的附件文件中找到单词?

java - groovy-wslite 支持 MTOM/附件吗?

java - XmlPullParser - 解析嵌套标记

java - 使用 commons-email 生成的附件+Html 在某些电子邮件客户端中不显示