如何通过电子邮件发送从Android应用程序生成的文件?

时间:2018-02-16 09:02:47

标签: android email android-studio pdf-generation email-attachments

我正在开发一个Android应用程序,其中我将生成一个pdf文件,我想将其作为电子邮件发送。以下是生成pdf文件的代码:

 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();
        }
    }

通过点击按钮帮我发送此文件。

1 个答案:

答案 0 :(得分:-1)

您可以在此处查看更多详细信息: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"));