Exporting and importing a database
suggest changeYou might want to import and export your database for bacukups for example. Dont forget about the permissions.
public void exportDatabase(){ try { File sd = Environment.getExternalStorageDirectory(); File data = Environment.getDataDirectory(); String currentDBPath = "//data//MY.PACKAGE.NAME//databases//MY_DATABASE_NAME"; String backupDBPath = "MY_DATABASE_FILE.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(c, c.getResources().getString(R.string.exporterenToast), Toast.LENGTH_SHORT).show(); } catch (Exception e) { Toast.makeText(c, c.getResources().getString(R.string.portError), Toast.LENGTH_SHORT).show(); Log.d("Main", e.toString()); } } public void importDatabase(){ try { File sd = Environment.getExternalStorageDirectory(); File data = Environment.getDataDirectory(); String currentDBPath = "//data//" + "MY.PACKAGE.NAME" + "//databases//" + "MY_DATABASE_NAME"; String backupDBPath = "MY_DATABASE_FILE.db"; 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(c, c.getResources().getString(R.string.importerenToast), Toast.LENGTH_LONG).show(); } catch (Exception e) { Toast.makeText(c, c.getResources().getString(R.string.portError), Toast.LENGTH_SHORT).show(); } }
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents