Click here to Skip to main content
15,949,741 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.

Java
Intent sendIntent = new Intent(Intent.ACTION_VIEW);
                sendIntent.setType("application/pdf");
                sendIntent.setData(Uri.parse("testuurmi@gmail.com"));
                sendIntent.setClassName("com.google.android.gm", "com.google.android.gm.ComposeActivityGmail");
                sendIntent.putExtra(Intent.EXTRA_EMAIL, new String[] { "testuurmi@gmail.com" });
                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);
                fos.write(bytes);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

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


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.
Posted
Updated 27-Jun-18 23:25pm

1 solution

please replace <pre>new Intent(Intent.ACTION_VIEW);
to
new Intent(Intent.ACTION_SEND);
and replace
sendIntent.setClassName("com.google.android.gm", "com.google.android.gm.ComposeActivityGmail");
to
sendIntent.setPackage("com.google.android.gm");
 
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