Last active
August 29, 2015 14:27
-
-
Save voghDev/ff28d15270a2e7f948e1 to your computer and use it in GitHub Desktop.
Helper method to copy your SQLite database to SD Card, in order to access it in non-rooted phones.
This file contains hidden or 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 class DBUtil { | |
| public static void copyDatabaseToSDCard(String dbName) { | |
| try { | |
| File sd = Environment.getExternalStorageDirectory(); | |
| File data = Environment.getDataDirectory(); | |
| if (sd.canWrite()) { | |
| String currentDBPath = "//data//"+ BuildConfig.APPLICATION_ID+"//databases//"+ dbName+".db"; | |
| String backupDBPath = dbName+"-backup.db"; | |
| File currentDB = new File(data, currentDBPath); | |
| File backupDB = new File(sd, backupDBPath); | |
| if (currentDB.exists()) { | |
| FileChannel src = new FileInputStream(currentDB).getChannel(); | |
| FileChannel dst = new FileOutputStream(backupDB).getChannel(); | |
| dst.transferFrom(src, 0, src.size()); | |
| src.close(); | |
| dst.close(); | |
| } | |
| } | |
| } catch (Exception e) { | |
| e.printStackTrace(); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment