0
  • 如何备份应用程序数据库并存储在移动存储
    文件夹中。我尝试这样做,但无法创建备份文件。

  try{
                File data = Environment.getDataDirectory();
                    File sd = new File(Environment.getExternalStorageDirectory()+"/digiplusBackUp");
                    if(!sd.exists()){
                        sd.mkdirs();
                    }
                    Calendar c = Calendar.getInstance();
                    SimpleDateFormat df1 = new SimpleDateFormat("dd-MMM-yyyy");
                    String formattedDate1 = df1.format(c.getTime());
                    SimpleDateFormat dateFormat = new SimpleDateFormat("hh:mm:ss");
                    String formattedDate3 = dateFormat.format(c.getTime());
                    if (sd.canWrite()) {
                    String currentDBPath = "//data//com.digiplus.live//databases//digiplus";
                    String backupDBPath = "DigiPlus"+formattedDate1+formattedDate3 ;
                    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) {
            Log.d("Error",ignored.tostring());
            }
            }

4

1 回答 1

0

这段代码解决了我的问题,谢谢 MikeT 的支持

try {
            File data = Environment.getDataDirectory();
            File sd = new File(Environment.getExternalStorageDirectory() + "/digiplusBackUp");
            if (!sd.exists()) {
                sd.mkdirs();
            }
            Calendar c = Calendar.getInstance();
            SimpleDateFormat df1 = new SimpleDateFormat("dd-MMM-yyyy");
            String formattedDate1 = df1.format(c.getTime());
            SimpleDateFormat dateFormat = new SimpleDateFormat("hh:mm:ss");
            String formattedDate3 = dateFormat.format(c.getTime());
            if (sd.canWrite()) {
                String currentDBPath = "/data/com.digiplus.lve/databases/digiplus.db";
                String backupDBPath = "backupFolder" + formattedDate1 + formattedDate3;
                File currentDB = new File(data, currentDBPath);
                File backupDB = new File(sd, backupDBPath);

                    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 e) {
            Log.d("Error", e.toString());
        }

    }

现在我想恢复备份,我该怎么做?

于 2018-06-21T07:11:59.013 回答