Created
February 13, 2019 14:26
-
-
Save raphaelbussa/e7553f80facdf3c9041b9132d246661f to your computer and use it in GitHub Desktop.
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
@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