Last active
January 2, 2016 11:49
-
-
Save huteri/8299315 to your computer and use it in GitHub Desktop.
Export database file
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 Uri exportDatabase() { | |
String dbName = DATABASE_NAME; | |
File dbFile = new File(DATABASE_NAME); | |
File desti = new File(Environment.getExternalStorageDirectory(), | |
"backup/"); | |
File file = new File(desti, dbName); | |
if (!desti.exists()) | |
desti.mkdirs(); | |
try { | |
file.createNewFile(); | |
copyFile(dbFile, file); | |
} catch (IOException e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
return null; | |
} | |
return Uri.fromFile(file); | |
} | |
private void copyFile(File src, File dst) throws IOException { | |
@SuppressWarnings("resource") | |
FileChannel inChannel = new FileInputStream(src).getChannel(); | |
@SuppressWarnings("resource") | |
FileChannel outChannel = new FileOutputStream(dst).getChannel(); | |
try { | |
inChannel.transferTo(0, inChannel.size(), outChannel); | |
} finally { | |
if (inChannel != null) | |
inChannel.close(); | |
if (outChannel != null) | |
outChannel.close(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment