如何在Android上将数据库文件备份到SD卡上?

我想添加一个function到我的Android应用程序,自动备份到SD卡的SQLite数据库。

最好的办法是什么呢? 是否有任何示例或教程可用?

SQLite数据库是完全自包含的文件,并且是可移植的 – 您可以直接将整个文件复制到SD卡。

虽然首先我会检查SD卡是否安装在设备中,以及它的path是什么(使用Environment.getExternalStorageDirectory() )。

这个代码适合我!

  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) { } 

有谁知道这是否会在非根电话上工作? 我只是试图在一个根深蒂固的G1。

 try { File sd = Environment.getExternalStorageDirectory(); File data = Environment.getDataDirectory(); if (sd.canWrite()) { String currentDBPath = "//data//"+ packageName +"//databases//"+dbList[0]; String backupDBPath = dbList[0]; 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(getBaseContext(), backupDB.toString(), Toast.LENGTH_LONG).show(); } } catch (Exception e) { Toast.makeText(getBaseContext(), e.toString(), Toast.LENGTH_LONG).show(); } 

这与上面的例子中的“/”是相反的,“/”浪费了我20分钟的时间来计算出来,但是我真的应该早一点看到这些。 Toast会告诉你文件放在哪里,或者告诉你什么是错误的,当它不起作用。

我用你可以放在你的SQLiteOpenHelper的方法来回答类似于这个问题。 这就像将db文件从某种外部存储复制到内部应用程序存储一样简单。 还有一些额外的代码打开并读取数据库文件,以确保它处于适当的状态,以便Android进行数据库调用。

 public static void BackupDatabase() throws IOException { boolean success =true; File file = null; file = new File(Environment.getExternalStorageDirectory() +"/MOLS_Backup"); if (file.exists()) { success =true; } else { success = file.mkdir(); } if (success) { String inFileName = "/data/data/com.sygic.sdk.demo/databases/MOLS_DB.s3db"; File dbFile = new File(inFileName); FileInputStream fis = new FileInputStream(dbFile); String outFileName = Environment.getExternalStorageDirectory()+"/MOLS_Backup/MOLS_DB.s3db"; // Open the empty db as the output stream OutputStream output = new FileOutputStream(outFileName); // Transfer bytes from the inputfile to the outputfile byte[] buffer = new byte[1024]; int length; while ((length = fis.read(buffer))>0) { output.write(buffer, 0, length); } output.flush(); output.close(); fis.close(); } } 

我不知道如果电话是根植的,但是你应该把你的文件写入:

 /Android/data/{package_name}/files/ 

无论是否植根,这都会起作用。

您必须在您的应用程序中授予权限android.permission.WRITE_EXTERNAL_STORAGE 。 它在无根设备上正常工作。

如果你是新手,你可以在数据库适配器中find你的数据库名称。

请注意,您也可以为SharedPreferences执行此操作,但请记住将Context.MODE_PRIVATE更改为Context.MODE_MULTI_PROCESS。

SharedPreferences_name应该看起来像这样= ExportSP("temp.xml");

 String currentPathForSharedPreferences = "/data/"+ context.getPackageName() +"/shared_prefs/"+ SharedPreferences_name; 

出口

 exportDB("MyDbName"); private void exportDB(String db_name){ File sd = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + File.separator + "Your Backup Folder"+ File.separator ); boolean success = true; if (!sd.exists()) { success = sd.mkdir(); } if (success) { File data = Environment.getDataDirectory(); FileChannel source=null; FileChannel destination=null; String currentDBPath = "/data/"+ context.getPackageName() +"/databases/"+db_name; String backupDBPath = db_name; File currentDB = new File(data, currentDBPath); File backupDB = new File(sd, backupDBPath); try { source = new FileInputStream(currentDB).getChannel(); destination = new FileOutputStream(backupDB).getChannel(); destination.transferFrom(source, 0, source.size()); source.close(); destination.close(); Toast.makeText(this, "Please wait", Toast.LENGTH_SHORT).show(); } catch(IOException e) { e.printStackTrace(); } }} 

为了import

 importDB("MyDbName"); private void importDB(String db_name){ File sd = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + File.separator + "Your Backup Folder"+ File.separator ); File data = Environment.getDataDirectory(); FileChannel source=null; FileChannel destination=null; String backupDBPath = "/data/"+ context.getPackageName() +"/databases/"+db_name; String currentDBPath = db_name; File currentDB = new File(sd, currentDBPath); File backupDB = new File(data, backupDBPath); try { source = new FileInputStream(currentDB).getChannel(); destination = new FileOutputStream(backupDB).getChannel(); destination.transferFrom(source, 0, source.size()); source.close(); destination.close(); Toast.makeText(this, "Please wait", Toast.LENGTH_SHORT).show(); } catch(IOException e) { e.printStackTrace(); } } 
 @Override protected void onCreate(Bundle savedInstanceState) { try { File sd = Environment.getExternalStorageDirectory(); File data = Environment.getDataDirectory(); if (sd.canWrite()) { String currentDBPath = "//data//"+getPackageName()+"//databases//"+DATABASE_NAME+""; String backupDBPath = "backup.db"; 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(getBaseContext(), backupDB.toString(), Toast.LENGTH_LONG).show(); } } catch (Exception e) { Toast.makeText(getBaseContext(), e.toString(), Toast.LENGTH_LONG).show(); } } 

如果成功阻止给我一个错误:

 String inFileName = context.getDatabasePath(DB-Name); File dbFile = new File(inFileName); FileInputStream fis = new FileInputStream(dbFile);