DonviYang 2013-10-08
public class ExportImportDB extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); //creating a new folder for the database to be backuped to File direct = new File(Environment.getExternalStorageDirectory() + "/Exam Creator"); if(!direct.exists()) { if(direct.mkdir()) { //directory is created; } } exportDB(); importDB(); } //importing database private void importDB() { // TODO Auto-generated method stub try { File sd = Environment.getExternalStorageDirectory(); File data = Environment.getDataDirectory(); if (sd.canWrite()) { String currentDBPath= "//data//" + "PackageName" + "//databases//" + "DatabaseName"; String backupDBPath = "/BackupFolder/DatabaseName"; File backupDB= new File(data, currentDBPath); File currentDB = 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(); } } //exporting database private void exportDB() { // TODO Auto-generated method stub try { File sd = Environment.getExternalStorageDirectory(); File data = Environment.getDataDirectory(); if (sd.canWrite()) { String currentDBPath= "//data//" + "PackageName" + "//databases//" + "DatabaseName"; String backupDBPath = "/BackupFolder/DatabaseName"; 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(); } } }
<uses-permissionandroid:name="android.permission.WRITE_EXTERNAL_STORAGE">
</uses-permission>