How to back up a database file on an Android SD card?

I would like to add a feature to my Android app that automatically backs up SQLite to an SD card .

What is the best way to do this? Are any examples or tutorials available?

+54
android database backup sd-card
Jan 03 '09 at 15:45
source share
11 answers

SQLite databases are fully standalone files and are portable and - you can simply copy the entire file directly to the SD card.

Although first I would check if the SD card is installed on the device and what its path is (using Environment.getExternalStorageDirectory() ).

+7
Jan 03 '09 at 17:21
source share

This code works for me!

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

Does anyone know if this will work on non-root phones? I just tried this on root G1.

+117
Apr 18 '10 at
source share
 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(); } 

This works in contrast to the examples above, in which "/" "wasted" 20 minutes of my life figuring this out, but I really should have seen it before. Toast will tell you where the file is, or let you know what is wrong when it does not work.

+18
Jan 18 2018-12-12T00:
source share

I answered a question like this with a method that you can place in your SQLiteOpenHelper . It is as simple as copying a db file from some external storage to the internal application storage. There is also additional code that opens and reads the db file to make sure that it is in the correct state for Android to make database calls with it.

+2
Jun 30 2018-11-11T00:
source share
 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(); } } 
+2
Jul 29 '13 at 11:48 on
source share

I don’t know what will happen if the phone is rooted or not, but you have to write your files:

 /Android/data/{package_name}/files/ 

This will work whether it is rooted or not.

+1
Sep 22 '10 at 3:03
source share

You must grant android.permission.WRITE_EXTERNAL_STORAGE permission in your application. It works great on untethered devices.

+1
Jul 27 2018-12-12T00:
source share

You will find your database name in the database adapter if you are new to this.

Note that you can also do this for SharedPreferences, but remember to change Context.MODE_PRIVATE to Context.MODE_MULTI_PROCESS.

SharedPreferences_name should look like this: ExportSP("temp.xml");

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

For export

 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(); } }} 

For 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(); } } 
0
May 01 '16 at 17:46
source share

@Skeniver code works for me. I just want to add the following:

Using:

 String currentDbPath = getApplicationContext().getDatabasePath("{database name}"); 

It will provide you the database path. It is better to use this instead of hardcoding String, for example:

 String currentDbPath = "//data//{package name}//databases//{database name}"; 
0
Dec 19 '17 at 14:13
source share
 @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(); } } 
-one
May 01 '17 at 13:03
source share

If the success block gives me an error:

 String inFileName = context.getDatabasePath(DB-Name); File dbFile = new File(inFileName); FileInputStream fis = new FileInputStream(dbFile); 
-2
Jan 23 '16 at 5:22
source share



All Articles