Click here to Skip to main content
15,849,594 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have an application which can open PDF via taping on app, openwith and also email attachment. I convert that file into inputstream and then to bytearray. Now I want this opened pdf to attach as attachment to gmail using Intent. I achieved this with the following code which adds pdf as attachment with no size, but received mail do not contain the attachment and a notification couldn't send attachment is displayed. Tried many ways.. but unsuccessful.. any help at the earliest. thanks in advance.

Intent sendIntent = new Intent(Intent.ACTION_VIEW);
                sendIntent.setClassName("", "");
                sendIntent.putExtra(Intent.EXTRA_EMAIL, new String[] { "" });
                sendIntent.putExtra(Intent.EXTRA_SUBJECT, "testPDF");
                sendIntent.putExtra(Intent.EXTRA_TEXT, "this is a PDF ");

                File pdfFile=null;
            try {
                pdfFile = new File(attachmentFileName);
                FileOutputStream fos = new FileOutputStream(pdfFile);
            } catch (FileNotFoundException e) {
            } catch (IOException e) {

                Uri fileuri = Uri.fromFile(pdfFile);
                 sendIntent.putExtra(Intent.EXTRA_STREAM, fileuri);
                //start the gmailapp intent

What I have tried:

I tried to use inputstream and bytearray already available with me to generate uri and attach. I dont want to physically write the file into SD card. Avaialble inputstream or bytearray should be converted to pdf and attach to gmail.
Updated 28-Jun-18 0:25am

1 solution

please replace <pre>new Intent(Intent.ACTION_VIEW);
new Intent(Intent.ACTION_SEND);
and replace
sendIntent.setClassName("", "");
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900