2015-04-16 37 views
0

我有一個數據庫,我想備份並恢復它。我用這個代碼來備份我的數據庫:備份數據庫並保存到特定文件夾android

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

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

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

     if (sd.canWrite()) { 
      String currentDBPath = "//data//jordanzimmittidevelopers.com.communityservicelogger//databases//community_service_Database"; 
      String backupDBPath = "Community Service Database"; 
      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) { 
    } 
} 

它備份數據庫不錯,但它保存到內部 SD卡的根目錄。我怎樣才能將它保存到由內部sd卡上的aoo創建的新文件夾中。由於

回答

1

使用下面的代碼,

File sd = new File(Environment.getExternalStorageDirectory()+"/newFolder"); 
if(!sd.exists()){ 
sd.mkdirs() 
} 

代碼是創造,如果不存在新的文件夾。然後複製數據庫。

+0

謝謝,這個作品 –

相關問題