2015-04-16 121 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

谢谢,这个作品 –

相关问题