从SD卡/外部存储Android恢复领域数据库

时间:2017-02-17 08:15:21

标签: android database copy realm restore

我遇到的情况是,我提示用户是否备份他们的消息,否则使用谷歌备份服务。我成功地将当前的域数据库备份到SD卡上,从那里进行备份到谷歌服务。 但是我想知道我想要将数据库从SD卡恢复到当前Realm实例的情况,这意味着将当前的realm文件替换为SD卡上可用的文件。

在旧版本中,我看到Realm提供了一种方法来指定领域将从中读取数据库文件的自定义路径,但在这个新的路径中,我看不到它。

请帮忙吗?

1 个答案:

答案 0 :(得分:1)

创建备份文件

       <script type="text/javascript">
      function datecalendar () {
        $.datepicker._clearDate('#datesearchdiv');
         $.datepicker._defaults.onAfterUpdate = null;


   var datepicker__updateDatepicker = $.datepicker._updateDatepicker;
      $.datepicker._updateDatepicker = function(inst) {
        datepicker__updateDatepicker.call(this, inst);

          var onAfterUpdate = this._get(inst, 'onAfterUpdate');
             if (onAfterUpdate) onAfterUpdate.apply((inst.input ? inst.input[0] : null), [(inst.input ? inst.input.val() : ''), inst]);
       }


           $(function() {

         var cur = -1,
         prv = -1;
          $('#datesearchdiv')
.datepicker({
  numberOfMonths: [12, 1],
  changeMonth: true,
  changeYear: true,
  minDate: 0,


  beforeShowDay: function(date) {
    return [true, ((date.getTime() >= Math.min(prv, cur) && date.getTime() <= Math.max(prv, cur)) ? 'date-range-selected' : '')];
  },

  onSelect: function(dateText, inst) {
    var d1, d2;

    prv = cur;
    cur = (new Date(inst.selectedYear, inst.selectedMonth, inst.selectedDay)).getTime();
    if (prv == -1 || prv == cur) {
      prv = cur;
      $('#departdate').val(dateText);
    } else {
      d1 = $.datepicker.formatDate('dd/mm/yy', new Date(Math.min(prv, cur)), {});
      d2 = $.datepicker.formatDate('dd/mm/yy', new Date(Math.max(prv, cur)), {});
      var d11 = $.datepicker.formatDate('mm/dd/yy', new Date(Math.min(prv, cur)), {});
      var d22 = $.datepicker.formatDate('mm/dd/yy', new Date(Math.max(prv, cur)), {});
      $('#departdate').val(d1);
      $('#returndate').val(d2);
      spandateupdate(d11, d22);
    }
  },

  onChangeMonthYear: function(year, month, inst) {
    //prv = cur = -1;
  },


})
.position({
  my: 'left top',
  at: 'left bottom',
  of: $('#departdate')
})
.hide();

            $('#departdate').on('focus', function(e) {
var v = this.value,
  d;

try {
  if (v.indexOf(' - ') > -1) {
    d = v.split(' - ');

    prv = $.datepicker.parseDate('mm/dd/yy', d[0]).getTime();
    cur = $.datepicker.parseDate('mm/dd/yy', d[1]).getTime();

  } else if (v.length > 0) {
    prv = cur = $.datepicker.parseDate('mm/dd/yy', v).getTime();
  }
} catch (e) {
  cur = prv = -1;
}

if (cur > -1) $('#datesearchdiv').datepicker('setDate', new Date(cur));

$('#datesearchdiv').datepicker('refresh').show();
       showdatespan();

     });

      });

       document.getElementById("departdate").focus();
       document.getElementById("departdate").select();
          }
              </script>
                   <script type="text/javascript">
                      function datecalendar1 () {
                  $.datepicker._defaults.onAfterUpdate = null;


      var datepicker__updateDatepicker = $.datepicker._updateDatepicker;
           $.datepicker._updateDatepicker = function(inst) {
     datepicker__updateDatepicker.call(this, inst);

    var onAfterUpdate = this._get(inst, 'onAfterUpdate');
         if (onAfterUpdate) onAfterUpdate.apply((inst.input ? inst.input[0] :    null), [(inst.input ? inst.input.val() : ''), inst]);
      }


       $(function() {

        var cur = -1,
              prv = -1;
          $('#datesearchdiv')
.datepicker({
  numberOfMonths: [12, 1],
  changeMonth: true,
  changeYear: true,
  minDate: 0,


  beforeShowDay: function(date) {
    return [true, ((date.getTime() === Math.min(cur)) ? 'date-range-selected' : '')];
  },

  onSelect: function(dateText, inst) {
    var d1;

    prv = cur;
    cur = (new Date(inst.selectedYear, inst.selectedMonth, inst.selectedDay)).getTime();
    if (prv == -1 || prv == cur) {
      prv = cur;
      $('#departdate').val(dateText);
    } else {
      d1 = $.datepicker.formatDate('dd/mm/yy', new Date(Math.min( cur)), {});
      var d11 = $.datepicker.formatDate('mm/dd/yy', new Date(Math.min( cur)), {});
      $('#departdate').val(d1);
      spandateupdate1(d11);
    }
  },

  onChangeMonthYear: function(year, month, inst) {
    //prv = cur = -1;
  },


})
.position({
  my: 'left top',
  at: 'left bottom',
  of: $('#departdate')
})
.hide();

       $('#departdate').on('focus', function(e) {
var v = this.value,
  d;

try {
  if (v.indexOf(' - ') > -1) {
    d = v.split(' - ');

    prv = $.datepicker.parseDate('mm/dd/yy', d[0]).getTime();
    cur = $.datepicker.parseDate('mm/dd/yy', d[1]).getTime();

  } else if (v.length > 0) {
    prv = cur = $.datepicker.parseDate('mm/dd/yy', v).getTime();
  }
} catch (e) {
  cur = prv = -1;
}

if (cur > -1) $('#datesearchdiv').datepicker('setDate', new Date(cur));

$('#datesearchdiv').datepicker('refresh').show();
showdatespan();

  });

    });
     document.getElementById("departdate").focus();
     document.getElementById("departdate").select();
   }
        </script>  

<强> RESTORE

public void backup() {
    try {
        // create a backup file
        File exportRealmFile;
        exportRealmFile = new File(EXPORT_REALM_PATH, EXPORT_REALM_FILE_NAME);

        // if backup file already exists, delete it
        exportRealmFile.delete();

        // copy current realm to backup file
        realm.writeCopyTo(exportRealmFile);

    } catch (IOException e) {
        e.printStackTrace();
    }
    realm.close();
}

希望得到这个帮助。

归功于link