Skip to content

Instantly share code, notes, and snippets.

@huteri
Last active January 2, 2016 11:49
Show Gist options
  • Save huteri/8299315 to your computer and use it in GitHub Desktop.
Save huteri/8299315 to your computer and use it in GitHub Desktop.
Export database file
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