尝试通过电子邮件发送 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/