Home > Net >  How do I share my application's Room database
How do I share my application's Room database

Time:10-15

I'd like to share my app's internal database as a SQLite file.
Here's my naïve attempt. It doesn't raise any Exception but never succeeds either, depending on the application I choose to share, I get "Unsupported attachment" (Telegram), "Upload was unsuccessful" (Drive), "Couldn't attach file" (Gmail), …

private fun shareData() {
    val dbPath = application.getDatabasePath(LOCAL_DB_NAME).path
    try {
        val shareIntent = Intent()
        shareIntent.action = Intent.ACTION_SEND
        shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(dbPath))
        shareIntent.type = "application/x-sqlite3"
        startActivity(Intent.createChooser(shareIntent, "Share Data"))
    } catch (e: Exception) {
        e.printStackTrace()
    }
}

CodePudding user response:

That is an invalid Uri in general. Even if you switched to Uri.forFile(), that would not help, as passing a file:// Uri between apps has been banned for several years.

Use FileProvider to share files between apps. FWIW, I cover FileProvider in this section of this free book. See also the documentation, the documentation, and the documentation.

  • Related