2013-04-03 5 views
2

Вот код для сохранения/копирования моих данных на SD-карту, когда я нажимаю кнопку «Резервная копия», моя база данных, сохраненная в sdcard в каталоге NOTEIT, и теперь я хочу восстановить эту базу данных, когда я нажимаю кнопку восстановления в свой каталог по умолчанию, поэтому кто-нибудь скажет мне, как это сделать?Как восстановить исходную директорию моей базы данных?

public static void backupDatabase() throws IOException 
{ 
    try 
    { 
    File dbFile = new File(Environment.getDataDirectory() + "/data/com.neelrazin.noteit/databases/data"); 

     File exportDir = new File(Environment.getExternalStorageDirectory()+"/NOTEIT"); 

     if (!exportDir.exists()) 
     { 
      exportDir.mkdirs(); 
     } 

     File file = new File(exportDir, dbFile.getName()); 

     file.createNewFile(); 

     FileChannel inChannel = new FileInputStream(dbFile).getChannel(); //fails here 

     FileChannel outChannel = new FileOutputStream(file).getChannel(); 

     try 
     { 
      inChannel.transferTo(0, inChannel.size(), outChannel); 
     } 
     finally 
     { 
      if (inChannel != null) 
      inChannel.close(); 
     if (outChannel != null) 
      outChannel.close(); 
     } 
    } 
    catch(Exception e) 
    { 
     e.printStackTrace(); 
    } 
} 
+0

Это может помочь вам http://stackoverflow.com/questions/9463686/how-to-move-a-file-from-sdcard-to-app -data-folder – Razin

ответ

0

Я считаю, что восстановление является полной противоположностью вашей резервной копии.

Как это:

public static void restoreDatabase() throws IOException 
{ 
    try 
    { 
    File dbFile = new File(Environment.getDataDirectory() + "/data/com.neelrazin.noteit/databases/data"); 

     File importDir = new File(Environment.getExternalStorageDirectory()+"/NOTEIT"); 

     if (!importDir.exists()) 
     { 
      throw new IOException("External 'NOTEIT' directory does not exist."); 
      return; 
     } 

     File file = new File(importDir, dbFile.getName()); 
     if (!file.exists())  
     { 
         throw new IOException("Does not exist external db file: NOTEIT/" + dbFile.getName()); 
         return; 
     } 

     FileChannel outChannel = new FileOutputStream(dbFile).getChannel(); 

     FileChannel inChannel = new FileInputStream(file).getChannel(); 

     try 
     { 
      inChannel.transferTo(0, inChannel.size(), outChannel); 
     } 
     finally 
     { 
      if (inChannel != null) 
      inChannel.close(); 
     if (outChannel != null) 
      outChannel.close(); 
     } 
    } 
    catch(Exception e) 
    { 
     e.printStackTrace(); 
    } 
} 
+0

Я пробовал это, но ничего не произошло – neel

+0

Я отредактировал ответ. Попробуй. –

+0

Я тоже пробовал, но все равно ничего не случилось ... – neel

Смежные вопросы