Skip to content

Instantly share code, notes, and snippets.

@Swisyn
Created November 30, 2016 20:48
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 Swisyn/fcee1eaf862aef6fc9d7084022680cd5 to your computer and use it in GitHub Desktop.
Save Swisyn/fcee1eaf862aef6fc9d7084022680cd5 to your computer and use it in GitHub Desktop.
Database Operations
public static void CopyDBIfNotExists(Context context, String dbName) {
File db = new File(context.getDatabasePath(dbName).getPath());
if (!db.exists()) {
File dbDirectory = new File("/data/data/" + context.getPackageName() + "/databases/");
dbDirectory.mkdirs();
try {
CopyDB(context.getAssets().open(dbName), new FileOutputStream(context.getDatabasePath(dbName).getPath()));
} catch (Exception e) {
e.printStackTrace();
}
}
}
private static void CopyDB(InputStream inputStream, OutputStream outputStream) {
byte[] buffer = new byte[1024];
int length;
try {
while ((length = inputStream.read(buffer)) > 0) outputStream.write(buffer, 0, length);
outputStream.flush();
inputStream.close();
outputStream.close();
Log.d("CopyDB", "> Database file has been copied successfully!");
} catch (Exception e) {
e.printStackTrace();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment