Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to attach pdf file in an email [closed]

Tags:

android

email

pdf

I want to attach a pdf file in an e-mail, I tried this code to send an email containing pdf file.

String to = textTo.getText().toString();
String subject = textSubject.getText().toString();
String message = textMessage.getText().toString();

Intent email = new Intent(Intent.ACTION_SEND);
email.putExtra(Intent.EXTRA_EMAIL, new String[]{ to});

email.putExtra(Intent.EXTRA_SUBJECT, subject);
email.putExtra(Intent.EXTRA_TEXT, message);             

email.setType("message/rfc822");
startActivity(Intent.createChooser(email, "Choose an Email client :"));
like image 285
mani Avatar asked Dec 11 '22 09:12

mani


2 Answers

Try this :

Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("application/pdf");
shareIntent.putExtra(Intent.EXTRA_EMAIL, new String[] { "[email protected]" });
shareIntent.putExtra(Intent.EXTRA_SUBJECT, "test " +    test);
shareIntent.putExtra(Intent.EXTRA_TEXT, "test"); 
shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///MyAPP/"+"test.pdf"));
startActivity(shareIntent);

Hope this helps.

like image 134
Siddharth_Vyas Avatar answered Dec 21 '22 11:12

Siddharth_Vyas


After searching around i found this to show how to store files to external memory

Developers Link

createExternalStoragePublicPicture();
File path = Environment.getExternalStoragePublicDirectory(
                                            Environment.DIRECTORY_PICTURES);
                                    File file = new File(path, "cards_01.pdf");
Intent intent = new Intent(Intent.ACTION_SEND ,Uri.parse("mailto:")); // it's not ACTION_SEND
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_SUBJECT, "Card Set ");
intent.putExtra(Intent.EXTRA_TEXT, "");
intent.putExtra(Intent.EXTRA_STREAM,Uri.fromFile(file));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // this will make such that when user returns to your app, your app is displayed, instead of the email app.
activity.startActivity(intent);

The link above shows how to delete the files also and says about putting them in correct folders to avoid overwriting other files. Hope this helps any others with same issues

like image 28
Mohit Rakhra Avatar answered Dec 21 '22 10:12

Mohit Rakhra