Skip to content

Instantly share code, notes, and snippets.

@jaisoni
Created July 27, 2018 07:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jaisoni/344d735c8c10fafbe7e185051d58c673 to your computer and use it in GitHub Desktop.
Save jaisoni/344d735c8c10fafbe7e185051d58c673 to your computer and use it in GitHub Desktop.
Function for fetch sqlite database file from data/data/ folder and copy it to device internal memory
public class DatabaseHelper extends SQLiteOpenHelper {
/**
* create a copy of database file from data/data to sdcard
*/
public static void dbBackup(@NonNull Context context) {
try {
String databaseName="myDb.db";
String outFileName="myDb" + Calendar.getInstance().getTimeInMillis() + ".db";
//Open your local db as the input stream
InputStream myInput = new FileInputStream(context.getDatabasePath(databaseName));
//Open the empty db as the output stream
OutputStream myOutput = new FileOutputStream(new File(Environment.getExternalStorageDirectory(), outFileName));
//transfer bytes from the inputfile to the outputfile
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer)) > 0) {
myOutput.write(buffer, 0, length);
}
//Close the streams
myOutput.flush();
myOutput.close();
myInput.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment