Monday, June 10, 2013

[android help] Android - create PDF and mail as attachment


Android - create PDF and mail as attachment



The main thing I am trying to do in my app is create a PDF file from data I have (table from ArrayList). After that being done, everything should be e-mailed as 1 attachment. The best way of doing this is by writing the file to the storage system en then passing it via mail, and afterwards deleting it again. Is it possible to create a File object or something like that and pass it immediately without having to write it to the file system? Also, caching is no option I believe because the mail app doesn't have access to my stored file from my app then.


For creating the PDF, I found a very nice and handy little thing called droidtext (http://code.google.com/p/droidtext/). I believe creating the PDF is no problem. However, the problems start at saving the file. Obviously I want to create the file on the external storage, as well as on the internal storage if the external storage is unavailable.


This is what I found for the part with creating the PDF. This piece of code only represents the internal storage, because I believe that is the problem. For external storage, I only need to work with Environment.getExternalStorageDirectory(), so that's no problem.



Document document = new Document();
try {
//hard coded for testing purpose
PdfWriter.getInstance(document, openFileOutput("test.pdf", Context.MODE_WORLD_WRITEABLE));//-->deprecated, why? Alternative?
document.open();
Table table = new Table(2, 2);
table.addCell("0.0");
table.addCell("0.1");
table.addCell("1.0");
table.addCell("1.1");
document.add(table);
document.add(new Paragraph("Table converted: "));
table.setConvert2pdfptable(true);
document.add(table);
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("plain/text");
//the line below is a complete mistery for me, I've tried a lot here...
intent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///test.pdf"));
intent.putExtra(Intent.EXTRA_EMAIL, new String[]{getResources().getString(R.string.mail_recipient)});
intent.putExtra(Intent.EXTRA_SUBJECT, getResources().getString(R.string.mail_subject));
intent.putExtra(Intent.EXTRA_TEXT, getResources().getString(R.string.mail_body1));
startActivity(Intent.createChooser(intent, "E-mail"));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (DocumentException e) {
e.printStackTrace();
}

document.close();


This code seems to work (I think) because it doesn't deliver errors in any kind. However, I can't locate the file on my device. I guess it should be in the root folder (/). Do the deprecated MODE_WORLD_READABLE and MODE_WORLD_WRITEABLE have an alternative?


Every time now the mailing app starts (gmail on my phone), the attachment can't be sent. After looking for a few hours around the internet, I found that the MODE_WORLD_XXX should be used in order to have access with your mailing app.


Also I am not sure if I should close the document before mailing or after. It doesn't change anything in the log.


Also, why is the MODE_WORLD_XXX deprecated? Are there alternatives?


I am terribly sorry for all the questions, but it's now passed 4 am here.


Thanks in advance, kind regards :)!



.

stackoverflow.comm

No comments:

Post a Comment

Google Voice on T-Mobile? [General]

Google Voice on T-Mobile? So I recently switched from a GNex on Verizon to a Moto X DE on T-Mobile. I had always used Google Voice for my v...