【问题标题】:how to email a file generated from an app in android?如何通过电子邮件发送从android中的应用程序生成的文件?
【发布时间】:2018-02-16 09:02:46
【问题描述】:

我正在开发一个 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();
        }
    }

点击按钮帮助我通过电子邮件发送此文件。

【问题讨论】:

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


【解决方案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"));

【讨论】:

  • .setType 显示错误,我需要为此导入任何包吗?
猜你喜欢
  • 1970-01-01
  • 2012-01-07
  • 1970-01-01
  • 1970-01-01
  • 2011-08-06
  • 1970-01-01
  • 2019-12-04
相关资源
最近更新 更多