Skip to content

Instantly share code, notes, and snippets.

@jldubz
Created July 14, 2015 18:41
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jldubz/9b34f24dbf8fc6095b7a to your computer and use it in GitHub Desktop.
Save jldubz/9b34f24dbf8fc6095b7a to your computer and use it in GitHub Desktop.
Slip a new SQLite database file into the application database path for an Android application. This can also be used to copy and paste any file to virtually any other location on an Android device.
try {
File appDbFile = getActivity().getApplicationContext().getDatabasePath("myDatabase");
File sourceDbFile = new File("/sdcard/downloads/myDatabase.db");
//Make sure the directory and file exist
File dir = new File("/data/data/" +getActivity().getPackageName() + "/databases/");
if (!dir.exists()) {
boolean result = dir.mkdir();
}
if (!appDbFile.exists()) {
boolean result = appDbFile.createNewFile();
}
//begin transfer
if (sourceDbFile.canRead()) {
if (appDbFile.canRead()) {
InputStream in = new FileInputStream(sourceDbFile);
OutputStream out = new FileOutputStream(appDbFile);
// Transfer bytes from in to out
byte[] buf = new byte[1024];
int len;
//Byte Firehose
//It goes ffffffsssssssshshhhhhhhhhhhhh!!!!!!!
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
in.close();
out.close();
Log.v(LOGTAG, "Finished YAY!!!!! CELEBRATIONS!!");
sourceReadyListener.onSourceReady();
} else {
Log.e(LOGTAG,"Error Accessing Database File");
}
}
}
catch(FileNotFoundException e) {
Log.e(LOGTAG, e.getMessage());
}
catch(IOException e) {
Log.e(LOGTAG, e.getMessage());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment