Created
July 27, 2018 07:52
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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