Reputation: 104
I am adding image to PDF file using itext and storing file on internal storage. I want to attach this file as an attachment to email. HOw can i do this , I tried but below code is not working
OutputStream fout = null;
Document document = null ;
try {
Bitmap screenshot = nChartView.createScreenshot();
Date dateVal = new Date();
String filename = String.valueOf(dateVal.getTime());
ByteArrayOutputStream stream = new ByteArrayOutputStream();
screenshot.compress(Bitmap.CompressFormat.PNG, 100, stream);
// fout.flush();
// fout.close();
File cacheDir = context.getCacheDir();
String tempPDFfile = cacheDir.getPath()+"/screenshot.pdf";
System.out.println("file Path"+tempPDFfile);
//Creating PDF and adding image to it
filename = String.valueOf(dateVal.getTime());
//String pdfFilePath = Environment.getExternalStorageDirectory().toString() + "/screenshotPDF"+filename+".pdf";
document = new Document();
PdfWriter.getInstance(document,new FileOutputStream(tempPDFfile));
document.open();
Image image = Image.getInstance(stream.toByteArray());
//if you would have a chapter indentation
int indentation = 0;
float scaler = ((document.getPageSize().getWidth() - document.leftMargin()
- document.rightMargin() - indentation) / image.getWidth()) * 100;
image.scalePercent(scaler);
document.add(image);
document.close();
File pdfFile = new File(tempPDFfile);
if(pdfFile.exists()){
System.out.println("true");
}
Toast.makeText(context, Constants.SCREENSHOT_SUCESS_MSG, Toast.LENGTH_LONG).show();
Intent i = new Intent(Intent.ACTION_SEND);
i.setType("plain/text");
i.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(pdfFile));
context.startActivity(Intent.createChooser(i, "E-mail"));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (DocumentException e) {
e.printStackTrace();
} finally {
}
Can anyone help regarding this ?
Upvotes: 0
Views: 627
Reputation: 1007296
Third-party email apps have no access to your internal storage. Either generate the PDF on external storage, or use FileProvider
to serve the PDF to email clients from your internal storage. The latter is a bit more complicated but is more secure.
Upvotes: 1