Home>

I want to refer to the pdf saved in the internal storage when the button is pressed and attach it to the activated gmail
(I want to attach automatically)

Current status

I am trying to attach a file based on the processing of the source code below.
Start gmail and [@@@ exist] is displayed, but no files are attached to gmail

Source code
button.setOnClickListener {
            val intent = Intent (Intent.ACTION_SEND)

            // Described when the TODO destination is set in advance
            intent.putExtra (Intent.EXTRA_SUBJECT, "Subject")
            intent.putExtra (Intent.EXTRA_TEXT, "text")
            createPdf ()
            intent.type = "application/pdf"
            val readFile = File (applicationContext.filesDir, "sample.pdf")
            if (readFile.exists ()) {
                Toast.makeText (this, "@@@ exist", Toast.LENGTH_SHORT) .show ()
                val attachments = Uri.parse (readFile.canonicalPath)
                intent.putExtra (Intent.EXTRA_STREAM, attachments)
                intent.type = "application/pdf"
            } else {
                Toast.makeText (this, "@@@ not-exist", Toast.LENGTH_SHORT) .show ()
            }
            if (Build.VERSION.SDK_INT>= Build.VERSION_CODES.DONUT) {
                intent.setPackage ("com.google.android.gm")
            }
            startActivity (intent)
        }
    }
Error code

After checking the log, it seems that no error log is output.

How can I attach it correctly?
If i know anyone, I would appreciate it if you could give me advice.

Addition (2019/11/22)

Renovation based on your advice,
Based on the blog you shared because FileUriExposedException occurred
I fixed it.

paths is specified as follows because there was a directory error.

<files-path name = "document" path = "." />


I checked the operation in the above state, but I was able to confirm the startup of Gmail and the existence of the file
The system message "attachment failed" and the following log were output.

Is the file path specified incorrectly?

E/Gmail: ComposeActivity: Error adding attachment
    fwy: FileNotFoundException when openAssetFileDescriptor.
        at fwz.a (PG: 2)
        at fwz.a (PG: 49)
        at dez.a (PG: 385)
        at def.run (PG: 2)
        at dez.a (PG: 35)
        at dez.a (PG: 613)
        at daw.a (PG)
        at aczs.a (PG: 6)
        at afaw.a (PG: 2)
        at afay.run (PG: 11)
        at afda.run (PG)
        at java.util.concurrent.Executors $RunnableAdapter.call (Executors.java:428)
        at acxm.run (PG: 2)
        at ablv.run (PG: 3)
        at android.os.Handler.handleCallback (Handler.java:751)
        at android.os.Handler.dispatchMessage (Handler.java:95)
        at android.os.Looper.loop (Looper.java:154)
        at android.app.ActivityThread.main (ActivityThread.java:6173)
        at java.lang.reflect.Method.invoke (Native Method)
        at com.android.internal.os.ZygoteInit $MethodAndArgsCaller.run (ZygoteInit.java:886)
        at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:776)

Thanks for your cooperation.