选择文件来恢复数据库android

时间:2015-04-22 20:36:19

标签: android backup

我有一个带有数据库的应用程序。我有一个备份数据库的方法,如下所示:

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

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

    // Get Calendar Instance//
    Calendar c = Calendar.getInstance();

    // Get Date//
    SimpleDateFormat mFormatter = new SimpleDateFormat("MM-dd-yy");

    // Create App Folder//
    File sd = new File(Environment.getExternalStorageDirectory()+"/C.S. Tracker Backups");
    if(!sd.exists()){
        sd.mkdirs();
    }
        try {
            File sd2 = Environment.getExternalStorageDirectory();
            File data = Environment.getDataDirectory();

            // Backup File//
            if (sd2.canWrite()) {
                String currentDBPath = "//data//jordanzimmittidevelopers.com.communityservicelogger//databases//community_service_Database";
                String backupDBPath = "/C.S. Tracker Backups/" + mFormatter.format(c.getTime());
                File currentDB = new File(data, currentDBPath);
                File backupDB = new File(sd2, backupDBPath);

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

另一种恢复数据库的方法如下:

// Method To Restore Database//
public void OnClick_Restore(View v) {

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

    // Get Calendar Instance//
    Calendar c = Calendar.getInstance();

    // Get Date//
    SimpleDateFormat mFormatter = new SimpleDateFormat("MM-dd-yy");

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

        if (sd.canWrite()) {
            String currentDBPath = "//data//jordanzimmittidevelopers.com.communityservicelogger//databases//community_service_Database";
            String backupDBPath = "/C.S. Tracker Backups/" + mFormatter.format(c.getTime());
            File currentDB = new File(data, currentDBPath);
            File backupDB = new File(sd, backupDBPath);

            if (currentDB.exists()) {
                FileChannel src = new FileInputStream(backupDB).getChannel();
                FileChannel dst = new FileOutputStream(currentDB).getChannel();
                dst.transferFrom(src, 0, src.size());
                src.close();
                dst.close();
                Toast.makeText(getApplicationContext(), "Database Restored successfully", Toast.LENGTH_SHORT).show();
            }
        }
    } catch (Exception ignored) {
    }
}

我遇到的问题是,当用户点击备份时,它会在手机上保存一个带有备份日期的文件。因此,如果有多个备份,我希望他们能够像弹出框一样单击要恢复的备份。感谢

编辑:声明如何执行此操作=因此,如果有多个备份,我希望他们能够像弹出框一样单击要恢复的备份。

1 个答案:

答案 0 :(得分:1)

 String backupDBPath = here get full path from filepicker; 

 restore (backupDBPath);

这是你的功能:

 public boolean restore(String backupDBPath) {

    File backupDB  = new File( backupDBPath);
    if (!backupDB.exists())
{
          Toast.makeText(getApplicationContext()
    , "file not found:\n"+backupDB.getAbsolutePath()
    , Toast.LENGTH_SHORT).show();

return false;
}

   File currentDB = getDatabasePath("community_service_Database");
   if (!currentDB.exists())
{
          Toast.makeText(getApplicationContext()
    , "file not found:\n"+currentDB.getAbsolutePath()
    , Toast.LENGTH_SHORT).show();

return false;
}

   try {

            FileChannel src = new FileInputStream(backupDB).getChannel();
            FileChannel dst = new FileOutputStream(currentDB).getChannel();
            dst.transferFrom(src, 0, src.size());
            src.close();
            dst.close();
            Toast.makeText(getApplicationContext(), "Database Restored successfully", Toast.LENGTH_SHORT).show();

        return true;

  } catch (Exception e) {
           Toast.makeText(getApplicationContext(), "Exception:\n"+e.getMessage()(), Toast.LENGTH_SHORT).show();

return false;
}

}