Как сделать резервную копию .db файлов на SDCard? - PullRequest
0 голосов
/ 22 июня 2011

Можно ли скопировать файл .db, такой как mmssms.db или user_dict.db, на SDCard?

Если это возможно, все, что мне нужно сделать, это прочитать файл БД и записатьэто на SDCard.

Ответы [ 2 ]

1 голос
/ 22 июня 2011
// Local database
    InputStream input = new FileInputStream(from);

    // create directory for backup
    File dir = new File(DB_BACKUP_PATH);
    dir.mkdir();

    // Path to the external backup
    OutputStream output = new FileOutputStream(to);

    // transfer bytes from the Input File to the Output File
    byte[] buffer = new byte[1024];
    int length;
    while ((length = input.read(buffer))>0) {
        output.write(buffer, 0, length);
    }

    output.flush();
    output.close();
    input.close();
1 голос
/ 22 июня 2011

Вы можете начать с этого образца:

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

   if (sd.canWrite()) {
      String currentDBPath = "\\data\\{package name}\\databases\\{database name}";
      String backupDBPath = "{database name}";
      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();
      }
   }
} catch (Exception e) {
   // exception
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...