How to attach database file to an Email in android?

我怕爱的太早我们不能终老 提交于 2019-12-01 14:27:26

The email program can't access your application's private files, in this case, the database you are trying to send. Copy the database to a folder in the SD card and attach it from there.

kikoso

You can copy the file to the SDCard using the following code:

File sd = Environment.getExternalStorageDirectory();
File data = Environment.getDataDirectory();
if (sd.canWrite()) {
    String currentDBPath = "\\data\\application.package\\databases\\name";
    String backupDBPath = "name";
    File currentDB = new File(data, currentDBPath);
    File backupDB = new File(sd, backupDBPath);
    if (currentDB.exists()) {
        FileChannel src;
    try {
    src = new FileInputStream(currentDB).getChannel();
        FileChannel dst = new FileOutputStream(backupDB).getChannel();
        try {
        dst.transferFrom(src, 0, src.size());
    src.close();
            dst.close();
        } catch (IOException e) {                                                    
            e.printStackTrace();
        }
    } catch (FileNotFoundException e) {
    e.printStackTrace();
    }

Remember to add the following permission: android:name="android.permission.WRITE_EXTERNAL_STORAGE"

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!