【问题标题】:Backup Database and save to specific folder android备份数据库并保存到特定文件夹android
【发布时间】:2023-03-15 10:27:01
【问题描述】:

我有一个数据库,我想备份和恢复它。我使用此代码备份我的数据库:

// Method To Backup Database//
public void OnClick_Backup(View v) {

    // Vibrates For 50 Mill//
    vibe.vibrate(50);

    try {
        File sd = Environment.getExternalStorageDirectory();
        File data = Environment.getDataDirectory();

        if (sd.canWrite()) {
            String currentDBPath = "//data//jordanzimmittidevelopers.com.communityservicelogger//databases//community_service_Database";
            String backupDBPath = "Community Service Database";
            File currentDB = new File(data, currentDBPath);
            File backupDB = new File(sd, backupDBPath);

            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();
                Toast.makeText(getApplicationContext(), "Backup is successful to SD card", Toast.LENGTH_SHORT).show();
            }
        }
    } catch (Exception ignored) {
    }
}

它可以很好地备份数据库,但将其保存到内部的根目录 SD卡。如何将数据库保存到内部 sd 卡上的 aoo 创建的新文件夹中。谢谢

【问题讨论】:

    标签: android database-backups


    【解决方案1】:

    使用下面的代码,

    File sd = new File(Environment.getExternalStorageDirectory()+"/newFolder");
    if(!sd.exists()){
    sd.mkdirs()
    }
    

    如果不存在,代码将创建新文件夹。然后复制数据库之后。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-11-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-14
      • 2014-01-13
      • 2014-11-04
      相关资源
      最近更新 更多