简体   繁体   中英

how to email a file generated from an app in android?

I'm developing an android app in which i'll be generating an pdf file which i want to sent as an email. Following is the code for generating pdf file:

 public void createPDF(View view) {
        Document doc = new Document();
        String outPath = Environment.getExternalStorageDirectory()+"/mypdf.pdf";
        try {
            PdfWriter.getInstance(doc,new FileOutputStream(outPath));
            doc.open();
            doc.add(new Paragraph(edttxt1.getText().toString()));
            doc.add(new Paragraph(txt.getText().toString()));
            doc.close();
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

Help me to email this file by clicking a button.

you can see more details here: How to send an email with a file attachment in Android

Intent emailIntent = new Intent(Intent.ACTION_SEND);
    emailIntent.setType("text/plain");
    emailIntent.putExtra(Intent.EXTRA_EMAIL, new String[] {"email@example.com"});
    emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Your subject");
    emailIntent.putExtra(Intent.EXTRA_TEXT, "body text");
    File root = Environment.getExternalStorageDirectory();
    String pathToMyAttachedFile = "/mypdf.pdf";
    File file = new File(root, pathToMyAttachedFile);
    if (!file.exists() || !file.canRead()) {
    return;
    }
    Uri uri = Uri.fromFile(file);
    emailIntent.putExtra(Intent.EXTRA_STREAM, uri);
    startActivity(Intent.createChooser(emailIntent, "Pick an Email provider"));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM