In my android app I have created a PDF that I would like the user to be able to send from my app through an email client. Right now the email client will show that the PDF is attached but is unable to send the attachment. Is this a permissions problem or something? Any suggestions?
EDIT stored to external by adding getExternalCacheDir()
and problem persists
Email function
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 function
@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();
}