Gmail error "Unable to attach file"
Asked Answered
R

1

8

I am trying to email PDF file using Gmail. However, Gmail app is showing toast:

Unable to attach file

PDF file is not-corrupt and is generated successfully in application's cache directory.

Code: (Please comment below if you need code in Java.):

    val photoURI: Uri = FileProvider.getUriForFile(this, "com.packagename.provider",
            File(this.cacheDir.path + "/Report.pdf"))

    val emailIntent = Intent(Intent.ACTION_SENDTO)
    emailIntent.data = Uri.parse("mailto:")
    emailIntent.putExtra(Intent.EXTRA_STREAM, photoURI)
    emailIntent.putExtra(Intent.EXTRA_EMAIL, arrayOf("[email protected]"))
    emailIntent.putExtra(Intent.EXTRA_SUBJECT, "subject")
    emailIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
    emailIntent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION)
    startActivity(emailIntent)

Please help

Rudich answered 8/12, 2017 at 0:7 Comment(8)
what is the size of the PDF file?Combo
less than 1 MB.Rudich
Gmail has read storage permission?Combo
You also need to give the user permission via a manifest file <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>Spagyric
Yes it has storage permission.Rudich
But I have stored file in Cache and letting client accessing it through FileProvider. Why is permission needed?Rudich
@MalwinderSingh Hi! I have the same problem. Could you tell me please how you solved this problem?Telescopic
@Telescopic You can use Gmail APIs for Android.Rudich
P
1

As per Android Documentation, you need to use the ACTION.SEND intent action in the Intent constructor for attachments. Common Intents - Android Documentation

ACTION_SENDTO (for no attachment) or ACTION_SEND (for one attachment) or ACTION_SEND_MULTIPLE (for multiple attachments)

Prase answered 17/2, 2019 at 14:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.