Created
July 14, 2015 18:41
-
-
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.
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
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