Created
November 30, 2016 20:48
-
-
Save Swisyn/fcee1eaf862aef6fc9d7084022680cd5 to your computer and use it in GitHub Desktop.
Database Operations
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 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