我正在尝试将通过滚动视图创建的 PDF 附加到电子邮件中。但是电子邮件发送时没有附加任何内容。没有显示错误消息。
public void emailPDF(View view){
PdfDocument document = getPDF();
ByteArrayOutputStream os = new ByteArrayOutputStream();
try{
document.writeTo(os);
document.close();
os.close();
}catch (IOException e){
throw new RuntimeException("Error generating file", e);
}
Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, "ammar5001@gmail.com");
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "report");
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, " ");
emailIntent.setType("application/pdf"); // accept any image
//attach the file to the intent
emailIntent.putExtra(Intent.EXTRA_STREAM, os.toByteArray() );
startActivity(Intent.createChooser(emailIntent, "Send your email in:"));
}
public PdfDocument getPDF(){
PdfDocument document = new PdfDocument();
PdfDocument.PageInfo pageInfo = new PdfDocument.PageInfo.Builder(300, 300, 1).create();
PdfDocument.Page page = document.startPage(pageInfo);
View content = findViewById(R.id.scrollView);
content.draw(page.getCanvas());
document.finishPage(page);
return document;
}