Skip to content

Instantly share code, notes, and snippets.

@madhankumardroid
Created July 21, 2018 14:54
Show Gist options
  • Save madhankumardroid/bd2de719326f10307962fbb6148225ca to your computer and use it in GitHub Desktop.
Save madhankumardroid/bd2de719326f10307962fbb6148225ca to your computer and use it in GitHub Desktop.
Save Database in SD Card
private void saveDBToSDCard() {
String databasePath = getActivity().getDatabasePath("Database.sqlite").getPath();
File f = new File(databasePath);
OutputStream myOutput = null;
InputStream myInput = null;
Log.d("testing", " testing db path " + databasePath);
Log.d("testing", " testing db exist " + f.exists());
if (f.exists()) {
try {
File directory = new File("/mnt/sdcard/DB_DEBUG");
if (!directory.exists())
directory.mkdir();
myOutput = new FileOutputStream(directory.getAbsolutePath()
+ "/" + "DB_Name.sqlite");
myInput = new FileInputStream(databasePath);
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer)) > 0) {
myOutput.write(buffer, 0, length);
}
myOutput.flush();
} catch (Exception e) {
} finally {
try {
if (myOutput != null) {
myOutput.close();
myOutput = null;
}
if (myInput != null) {
myInput.close();
myInput = null;
}
} catch (Exception e) {
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment