Gmail“无法附加空文件”

时间:2015-06-25 23:32:30

标签: android email-attachments

我的应用使用iText库创建PDF(带有填写表格的模板PDF),然后我想将其附加到要发送的电子邮件中。当我尝试附加该文件时,我在Gmail应用中收到错误Can't attach empty file。还试过HTC电子邮件应用程序和Touchdown电子邮件应用程序 - 两者都没有附加任何内容并且没有错误。

创建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();
}

尝试附加文件并发送电子邮件:

public void sendMail(){
    Intent emailIntent = new Intent(Intent.ACTION_SEND);
    emailIntent .setType("*/*");
    emailIntent .putExtra(Intent.EXTRA_EMAIL, "blah@gmail.com");
    emailIntent .putExtra(Intent.EXTRA_STREAM, Uri.parse(dest));
    emailIntent .putExtra(Intent.EXTRA_SUBJECT, "Subject");
    startActivity(Intent.createChooser(emailIntent, "Send email..."));
}

其中dest是相关文件的路径和文件名。我知道dest中的路径和文件名是正常的,因为这是用于保存文件的var(我可以在设备和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)

2 个答案:

答案 0 :(得分:32)

我遇到了类似的问题。上面CommonsWare的评论解决了它。!

使用Uri.fromFile()方法创建一个File对象并生成一个Uri。

.row1{
    width: 100%;
}
.row1 span{
    background-color: #ff0000;
    padding:10px 20px 10px 20px;
    display: inline-block;
    vertical-align: top;
    color:white;
    width: 14%;
}
.lista{
    display: inline-block;
    vertical-align: top;
    width:71%;
}
.row1 ul{
    display: inline-block;
    margin-top: 13px;
}
.row1 ul li{
    display: inline-block;
    vertical-align: top;
    font-size: 0.7em;
    margin-left: 1px;
}
.row1 ul li a {
        color:#333;
}
.row1 ul li:before{
content:'|';
padding-right: 5px;
padding-left: -2px;
}
.row1 ul li:first-child:before{
    content:none !important;
}

答案 1 :(得分:2)

我在使用Android 6的Nexus 5上遇到了同样的问题(相同的代码在我的Nexus 6上使用Android 6),在此处找到解释和工作解决方案:https://stackoverflow.com/a/32982050/534898