My app creates a PDF using iText library (template PDF with forms which are filled) and I want to then attach it to an email to send. When I try to attach the file I get the error Can't attach empty file
in the Gmail app. Have also tried with the HTC email app and Touchdown email app - both of which just don't attach anything and give no error.
Creating the PDF:
public void manipulatePdf(String src, String dest) throws IOException, DocumentException {
PdfReader reader = new PdfReader(src);
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
AcroFields form = stamper.getAcroFields();
// Fill all PDF forms here (theres quite a few)
stamper.setFormFlattening(true);
stamper.close();
reader.close();
}
Attempting to attach file and send email:
public void sendMail(){
Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent .setType("*/*");
emailIntent .putExtra(Intent.EXTRA_EMAIL, "[email protected]");
emailIntent .putExtra(Intent.EXTRA_STREAM, Uri.parse(dest));
emailIntent .putExtra(Intent.EXTRA_SUBJECT, "Subject");
startActivity(Intent.createChooser(emailIntent, "Send email..."));
}
Where dest
is the path and filename of the file in question. I know the path and filename in dest
are ok as this is the var used to save the file (which I can view fine on both the device and on a PC).
Gmail logcat:
06-26 00:24:48.901 14476-14476/? E/Gmail﹕ Error adding attachment
com.android.mail.utils.b: Cannot attach empty attachment
at com.android.mail.ui.ComposeAttachmentTileGrid.a(SourceFile:62)
at com.android.mail.compose.c.b(SourceFile:1933)
at com.android.mail.compose.c.c(SourceFile:2063)
at com.android.mail.compose.c.a(SourceFile:7992)
at com.android.mail.compose.c.q(SourceFile:759)
at com.android.mail.compose.c.onCreate(SourceFile:4830)
at com.google.android.gm.ComposeActivityGmail.onCreate(SourceFile:194)
at android.app.Activity.performCreate(Activity.java:5958)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1129)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2364)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2474)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1359)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:155)
at android.app.ActivityThread.main(ActivityThread.java:5696)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1028)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823)
Gmail needs storage permissions to access and attach files from your mobile device. Step 1: Open Settings > Apps > Gmail > Permissions > Storage. Make sure it is set to Allow so Gmail can access files on your phone and attach it.
You can send Adobe PDF files directly from Adobe Acrobat or Acrobat Reader using Gmail. To do this, you have to allow access to your Gmail account. For security reasons, your administrator might have disabled this option. Note: Your company's IT policies must allow you to send files from Acrobat using webmail.
Clear the Cache and Data of Message App. The old cache files of your messaging app on your phone may be the reason behind the problem of unable to attach files. You can clear the cache and data of the messaging app you use to resolve the error of unable to attach files in the following way.
I was facing a similar issue. CommonsWare's comment above resolved it.!
Create a File object and generate a Uri using Uri.fromFile() method.
File file = new File(filePath);
Uri uri = Uri.fromFile(file);
I ran into the same problem on Nexus 5 with Android 6 (the same code worked on my Nexus 6 with Android 6), found explanation and working solution here: https://stackoverflow.com/a/32982050/534898
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With