【发布时间】:2015-11-06 19:27:11
【问题描述】:
在我的 android 应用程序中,我创建了一个 PDF,我希望用户能够通过电子邮件客户端从我的应用程序发送该 PDF。现在电子邮件客户端将显示 PDF 已附加但无法发送附件。这是权限问题还是什么?有什么建议吗?
EDIT通过添加getExternalCacheDir() 存储到外部并且问题仍然存在
电子邮件功能
private void EmailDialog(){
//create a dialog that prompts the user whether or not they want to send the PDF
dialogBuilder = new AlertDialog.Builder(this);
final TextView txtInput = new TextView(this);
dialogBuilder.setTitle("Email Audit");
dialogBuilder.setMessage("Do you want to email this audit?");
dialogBuilder.setView(txtInput);
dialogBuilder.setPositiveButton("Add", new DialogInterface.OnClickListener() {
//allows user to send PDF over their email application choice
@Override
public void onClick(DialogInterface dialog, int which) {
Intent i = new Intent(Intent.ACTION_SEND);
i.setType("message/rfc822");
PDF = new File(getExternalCacheDir() + PDFname);
i.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(PDF));
try {
startActivity(Intent.createChooser(i, "Send mail..."));
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(OldLocation.this, "There are no email clients installed.", Toast.LENGTH_SHORT).show();
}
}
});
dialogBuilder.setNegativeButton("Delete", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
RemoveDialog();
}
});
AlertDialog emailDialog = dialogBuilder.create();
emailDialog.show();
}
PDF功能
@TargetApi(Build.VERSION_CODES.KITKAT)
private void createPDF(){
// open a new document
PrintAttributes pa = new PrintAttributes.Builder().setMediaSize(PrintAttributes.MediaSize.NA_LETTER).setMinMargins(PrintAttributes.Margins.NO_MARGINS).build();
PrintedPdfDocument document = new PrintedPdfDocument(this, pa);
// start a page
PdfDocument.Page page = document.startPage(0);
// draw something on the page
View content = textView;
content.draw(page.getCanvas());
// finish the page
document.finishPage(page);
// add more pages
// write the document content
try {
File PDFs = getExternalCacheDir();
File file = new File(PDFs, companyInfo.getName() + ".pdf");
file.createNewFile();
FileOutputStream fos = new FileOutputStream(file);
document.writeTo(fos);
fos.close();
//close the document
document.close();
} catch (Exception ex){
}
realm.beginTransaction();
companyInfo = realm.where(CompanyInfo.class).findFirst();
companyInfo.removeFromRealm();
realm.commitTransaction();
}
【问题讨论】:
-
哪个邮件客户端发送失败?
-
Gmail 和谷歌浏览器也无法保存
-
可能是文件位置被拒绝。 This question 有类似的问题,通过将文件转移到公共区域解决了
标签: java android email pdf android-studio