4

I am having trouble sending an attachment of email.

Firstly: I am using iText5 to stamp some text to a PDF. The PDF is stored in my assets folder:

AssetManager am = getAssets();
InputStream fs;
FileOutputStream fOut  ;

try
{
    fs = am.open("RFA2_10_11.pdf");
    PdfReader pdfReader = new PdfReader(fs);
    Log.d("pdfreader", pdfReader.getFileLength()+"   kello");
    fOut = openFileOutput("DocStamped.pdf",
    MODE_WORLD_READABLE);

    PdfStamper stamper = new PdfStamper(pdfReader, fOut);
    PdfContentByte canvas = stamper.getOverContent(1);

    ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT, new Phrase("Stefano Molle"), 250, 710, 0);
    ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT, new Phrase("4 Saint Pappins Road, Glasnevin, Dublin 11"), 250, 645, 0);

    stamper.close();
}
catch (IOException e) 
{
    // TODO Auto-generated catch block
    e.printStackTrace();
}
catch (DocumentException e) 
{
    // TODO Auto-generated catch block
    e.printStackTrace();
}

So the PDF that is created is stored in the root Files Directory of my application.

I need to send the saved DocStamped.pdf file as an attachment in an email:

String emailTo = "example@example.com";
//lets send the email

final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND_MULTIPLE);
emailIntent.setType("text/plain");
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{emailTo});            
ArrayList<Uri> uris = new ArrayList<Uri>();

File file = new File(getFilesDir()+"/"+"DocStamped.pdf");
Log.d("file", file.getAbsolutePath());

Uri uri =  Uri.fromFile(file);
uris.add(uri);
emailIntent.putExtra(Intent.EXTRA_STREAM, uri);

// emailIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
startActivity(Intent.createChooser(emailIntent, "Send mail..."));

Now this is my code to send the email. but I am having trouble. On the app it says there is an attachment, but when I check my email account within the browser, there is no attachment just the text I write in.

Any ideas guys why the attachment is not sending?

Joris Schellekens
  • 8,483
  • 2
  • 23
  • 54
molleman
  • 2,934
  • 16
  • 61
  • 92

2 Answers2

0

Your code works for me, with a couple of modifications:

  • I use getFilesDir() (/data/data/[app name]/files) to retrive the pdf instead of assets
    • Insted of ArrayList<Uri> I use a single Uri, like this:

Uri URI = Uri.parse("file://" + file.getAbsolutePath()); emailIntent.putExtra(Intent.EXTRA_STREAM, URI);

athospy
  • 534
  • 8
  • 16
-1

if you are using gmail client, the attachment path must start with mnt/sdcard.

mihail
  • 2,173
  • 19
  • 31
  • But i dont think the app is storing the file on the sd card, – molleman Nov 30 '11 at 09:39
  • Ok, but this is the restriction of the gmail client. If you want to support the gmail client, then files must be attached from the sdcard – mihail Nov 30 '11 at 09:47