Skip to content

Instantly share code, notes, and snippets.

@raphaelbussa
Created February 13, 2019 14:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save raphaelbussa/e7553f80facdf3c9041b9132d246661f to your computer and use it in GitHub Desktop.
Save raphaelbussa/e7553f80facdf3c9041b9132d246661f to your computer and use it in GitHub Desktop.
@Throws(Exception::class)
fun Realm.exportDatabase(context: Context) {
val path = "${Environment.getExternalStorageDirectory().path}/${this.configuration.realmFileName}"
val file = File(path)
if (file.exists()) {
file.delete()
}
this.writeCopyTo(file)
this.close()
val uri = FileProvider.getUriForFile(context, BuildConfig.APPLICATION_ID + ".provider", file)
val name = context.packageManager.getApplicationLabel(context.applicationInfo)
val intent = Intent(Intent.ACTION_SEND).apply {
type = "text/plain"
putExtra(Intent.EXTRA_SUBJECT, "Realm export for \"$name\"")
putExtra(Intent.EXTRA_STREAM, uri)
addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
}
context.startActivity(intent)
this.close()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment