Android Intent To Send And Share File

April 14, 2018

Setup FileProvider by editing AndroidManifest.xml

<manifest ...>

    <application ...>
                android:resource="@xml/file_paths" />

Specify sharable directories by editing res/xml/file_paths.xml

<paths xmlns:android="">
    <!-- everything -->
    <cache-path name="all" path="." />

    <cache-path name="images" path="/images" />

Create intent to send/share file.

val uri = FileProvider.getUriForFile(context, "com.mydomain.myapp.fileprovider", outputFile)

val shareIntent = Intent(Intent.ACTION_SEND)
shareIntent.type = "application/octet-stream"
// intent.type = "image/jpeg"
shareIntent.putExtra(Intent.EXTRA_STREAM, uri)
startActivityForResult(Intent.createChooser(shareIntent., "Backup"), BACKUP_FILE_REQUEST_CODE)

NOTE: I believe it’s not possible to share database file as you will get IllegalArgumentException: Failed to find configured root that contains /data/data/com.mydomain.myapp/databases/*. The solution to copy the database file to cache directory before sharing.

Check if intent is successful or fail.

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    if (requestCode == BACKUP_FILE_REQUEST_CODE) {
        if (resultCode == Activity.RESULT_OK) {
            // success
        else if (resultCode == Activity.RESULT_CANCELLED) {
            // cancelled


This work is licensed under a
Creative Commons Attribution-NonCommercial 4.0 International License.