Search code examples
androidkotlinpdfshareandroid-implicit-intent

How can I share pdf file in android?


I try to share a pdf file from android/data/mypackage/files/file.pdf I generate these pdfs also in this app, and when I try to share it, the pdf doesn't appear in on attached files from email, or google drive says something like: "No data to share". Here is my code for sharing pdf:

            val aName = intent.getStringExtra("iName")
            val file = File(this.getExternalFilesDir(null)?.absolutePath.toString(), "$aName")
            val shareIntent = Intent(Intent.ACTION_SEND)
            shareIntent.putExtra(Intent.EXTRA_STREAM,  file)
            shareIntent.flags = Intent.FLAG_GRANT_READ_URI_PERMISSION
            shareIntent.type = "application/pdf"
            startActivity(Intent.createChooser(shareIntent, "share.."))
            Toast.makeText(this,"$file",Toast.LENGTH_SHORT).show()

The pdf path looks correct when I toast it: SS


Solution

  • The problem is that you are not using a URI, just sending a path, you need several things.

    Provider paths

    You have to create provider_paths.xml under xml folder in res :

    <?xml version="1.0" encoding="utf-8"?>
    <paths xmlns:android="http://schemas.android.com/apk/res/android">
        <external-files-path
            name="files_root"
            path="/" />
    </paths>
    

    Set the provider in the Manifest under Aplication:

    <provider
      android:name="androidx.core.content.FileProvider"
      android:authorities="${applicationId}.provider"
      android:exported="false"
      android:grantUriPermissions="true">
         <meta-data
           android:name="android.support.FILE_PROVIDER_PATHS"
           android:resource="@xml/provider_paths" />
    </provider>
    

    Get the URI

    fun uriFromFile(context:Context, file:File):Uri {
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
      {
        return FileProvider.getUriForFile(context, BuildConfig.APPLICATION_ID + ".provider", file)
      }
      else
      {
        return Uri.fromFile(file)
      }
    }
    

    Your final code:

    val aName = intent.getStringExtra("iName")
                val shareIntent = Intent(Intent.ACTION_SEND)
                shareIntent.putExtra(Intent.EXTRA_STREAM,  uriFromFile(context,File(this.getExternalFilesDir(null)?.absolutePath.toString(), "$aName")))
                shareIntent.flags = Intent.FLAG_GRANT_READ_URI_PERMISSION
                shareIntent.type = "application/pdf"
                startActivity(Intent.createChooser(shareIntent, "share.."))
    

    I didn't test the code, write it from "memory", let me know if it works for you.