0

I just started using text for an app I am working on to further my android knowledge. However what I cannot figure out is how to get the pdf which was filled and then email it using the intent. I have been googling and researching everywhere but I cannot find anything. Does anyone know how to go about it?

declaration of my file before my onCreate;

File file = new File(getExternalFilesDir(null),"pvgform.pdf");

This is part of my code to add info to the fillable pdf text

OutputStream output = null;
try {
    output = new FileOutputStream(file);
} catch (FileNotFoundException e) {
    e.printStackTrace();
}
try {
    reader = new PdfReader(getResources().openRawResource(R.raw.form));
} catch (IOException e) {
    e.printStackTrace();
}
try {
    PdfStamper stamper = new PdfStamper(reader, output);
    AcroFields acroFields = stamper.getAcroFields();
    acroFields.setField("fullname", editText.getText().toString());
    acroFields.setField("agedob", editText2.getText().toString());
    acroFields.setField("description", editText3.getText().toString() + editText4.getText().toString() + editText5.getText().toString());
    acroFields.setField("duration", editText6.getText().toString());
    acroFields.setField("brandname", editText8.getText().toString());
    acroFields.setField("genericname", editText9.getText().toString());
    stamper.setFormFlattening(true);
    stamper.close();
} catch (DocumentException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}
reader.close();

and this is my attempt on the email intent for the pdf form:

Intent email = new Intent(Intent.ACTION_SEND);
email.putExtra(Intent.EXTRA_SUBJECT, "My form");
email.putExtra(Intent.EXTRA_TEXT, "Here is a form");
Log.d("file",file.getAbsolutePath());
Uri uri = Uri.fromFile(file);
email.putExtra(Intent.EXTRA_STREAM,uri);
email.setType("application/pdf");
email.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

when I run this, there is no attachment to the email to be sent.

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
Aria
  • 389
  • 3
  • 7
  • 25

1 Answers1

0

This is part of my code to add info to the fillable pdf text

You do not appear to be writing the PDF to a file.

and this is my attempt on the email intent for the pdf form:

Uri uri=Uri.parse("file://"+ "form.pdf");

This is not a valid Uri on any platform.

Write your PDF to a file, such as on external storage. Then, use a Uri that resolves to that file, using Uri.fromFile() to convert your File object into the appropriate Uri.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • isn't opening the stamper and adding user input text adding it to the fillable text? also, i have edited my uri. is it better? – Aria Sep 25 '15 at 20:36
  • @Aria: "isn't opening the stamper and adding user input text adding it to the fillable text?" -- yes, but only in memory. "also, i have edited my uri. is it better?" -- first, I do not see any code where you are saving the PDF to that file. Second, use `new File(getFilesDir(), "form.pdf")` to more safely construct that path. Third, that `Uri` will be useless, as third-party email apps cannot read from your app's internal storage. You can save the file to `getFilesDir()`, but then you would need to use `FileProvider` to serve the file in a way that third-party apps could access it. – CommonsWare Sep 25 '15 at 20:39
  • so could you tell me what to add to my code? I'm sorry I'm a beginner with text so I don't know what else to try – Aria Sep 25 '15 at 20:40
  • @Aria: Presumably, instead of `OutputStream output = null`, you will be better served with `OutputStream output = new FileOutputStream(yourFile);`, where `yourFile` is a `File` object pointing to where you want the PDF to be written. – CommonsWare Sep 25 '15 at 20:44
  • @Aria: You now appear to be writing the file to a location on internal storage. The third-party email app will not be able to read it. The simplest fix is to switch `getFilesDir()` to `getExternalFilesDir(null)`. Also, change `Uri.parse("file://"+ file.getAbsolutePath())` to `Uri.fromFile(file)`. – CommonsWare Sep 25 '15 at 21:14
  • Ok @CommonsWare after debugging, i ran what is in the question exactly, but it gave a nullpointerexception on the File declaration, and crashed on the emulator – Aria Sep 25 '15 at 22:44
  • @Aria: At this point, there is not much that I do to help you, sorry. – CommonsWare Sep 25 '15 at 22:54