Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
@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