Permalink
Find file
926cbb0 Oct 8, 2016
28 lines (25 sloc) 1.08 KB
public static SQLiteDatabase initDatabase(Activity activity, String databaseName){
try {
String outFileName = activity.getApplicationInfo().dataDir + "/databases/" + databaseName;
File f = new File(outFileName);
if(!f.exists()) {
InputStream e = activity.getAssets().open(databaseName);
File folder = new File(activity.getApplicationInfo().dataDir + "/databases/");
if (!folder.exists()) {
folder.mkdir();
}
FileOutputStream myOutput = new FileOutputStream(outFileName);
byte[] buffer = new byte[1024];
int length;
while ((length = e.read(buffer)) > 0) {
myOutput.write(buffer, 0, length);
}
myOutput.flush();
myOutput.close();
e.close();
}
} catch (IOException e) {
e.printStackTrace();
}
return activity.openOrCreateDatabase(databaseName, Context.MODE_PRIVATE, null);
}