我在发送电子邮件附件时遇到问题。
首先:我正在使用 iText5 将一些文本标记为 PDF。 PDF 存储在我的 Assets 文件夹中:
AssetManager am = getAssets();
InputStream fs;
FileOutputStream fOut ;
try
{
fs = am.open("RFA2_10_11.pdf");
PdfReader pdfReader = new PdfReader(fs);
Log.d("pdfreader", pdfReader.getFileLength()+" kello");
fOut = openFileOutput("DocStamped.pdf",
MODE_WORLD_READABLE);
PdfStamper stamper = new PdfStamper(pdfReader, fOut);
PdfContentByte canvas = stamper.getOverContent(1);
ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT, new Phrase("Stefano Molle"), 250, 710, 0);
ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT, new Phrase("4 Saint Pappins Road, Glasnevin, Dublin 11"), 250, 645, 0);
stamper.close();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (DocumentException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
因此创建的 PDF 存储在我的应用程序的根文件目录中。
我需要将保存的 DocStamped.pdf 文件作为电子邮件附件发送:
String emailTo = "example@example.com";
//lets send the email
final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND_MULTIPLE);
emailIntent.setType("text/plain");
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{emailTo});
ArrayList<Uri> uris = new ArrayList<Uri>();
File file = new File(getFilesDir()+"/"+"DocStamped.pdf");
Log.d("file", file.getAbsolutePath());
Uri uri = Uri.fromFile(file);
uris.add(uri);
emailIntent.putExtra(Intent.EXTRA_STREAM, uri);
// emailIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
startActivity(Intent.createChooser(emailIntent, "Send mail..."));
现在这是我发送电子邮件的代码。但我遇到了麻烦。在应用程序上它说有一个附件,但是当我在浏览器中查看我的电子邮件帐户时,没有附件只有我写的文本。
大家知道为什么附件发送不出去吗?
最佳答案
您的代码对我有用,只需进行一些修改:
- 我使用 getFilesDir() (/data/data/[app name]/files) 来检索 pdf 而不是 Assets
- 代表
ArrayList<Uri>
我使用单个 Uri,如下所示:
- 代表
Uri URI = Uri.parse("file://" + file.getAbsolutePath());
emailIntent.putExtra(Intent.EXTRA_STREAM, URI);
关于java - 在 Android 中将 PDF 作为电子邮件附件发送,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8323656/