不能将PDF到邮件机器人 [英] Can not attach pdf into mail in android

查看:294
本文介绍了不能将PDF到邮件机器人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个PDF并通过电子邮件发送。现在,我已经成功完成PDF生成。当我尝试的PDF附加到电子邮件和发送,我得到错误。

I am trying to create a pdf and send via email. Now I have sucessfully completed the pdf generation. When I try to attach the pdf into mail and send, I am getting error.

我想是这样的:

private void sendMail(Uri URI) {
        try {
            // Uri URI = null;

            String email = emailFromDB;

            String subject = "Report For the day " + getDateTime();

            String message = "Report For the day " + getDateTime();

            final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
            emailIntent.setType("application/pdf");
            emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL,new String[] { email });
            emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,subject);

            if (URI != null) {
                emailIntent.putExtra(Intent.EXTRA_STREAM, URI);
            }

            emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, message);
            emailIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            this.startActivity(Intent.createChooser(emailIntent,"Sending email..."));

        } catch (Throwable t) {

            Toast.makeText(this,

            "Request failed try again: " + t.toString(),

            Toast.LENGTH_LONG).show();

        }

    }

我得到这个erroe当我尝试发送。

I am getting this erroe when I try to send.

05-07 22:51:08.616: E/Gmail(326): java.io.FileNotFoundException: No content provider: /mnt/sdcard/MyMenu/2015-05-07 22-50-39-Report.pdf
05-07 22:51:08.616: E/Gmail(326):   at android.content.ContentResolver.openTypedAssetFileDescriptor(ContentResolver.java:604)
05-07 22:51:08.616: E/Gmail(326):   at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:536)
05-07 22:51:08.616: E/Gmail(326):   at android.content.ContentResolver.openFileDescriptor(ContentResolver.java:449)
05-07 22:51:08.616: E/Gmail(326):   at com.android.mail.compose.f.b(SourceFile:2817)
05-07 22:51:08.616: E/Gmail(326):   at com.android.mail.compose.f.a(SourceFile:3484)
05-07 22:51:08.616: E/Gmail(326):   at com.google.android.gm.ComposeActivityGmail.a(SourceFile:560)
05-07 22:51:08.616: E/Gmail(326):   at com.android.mail.compose.f.a(SourceFile:3122)
05-07 22:51:08.616: E/Gmail(326):   at com.android.mail.compose.f.w(SourceFile:2559)
05-07 22:51:08.616: E/Gmail(326):   at com.android.mail.compose.f.onOptionsItemSelected(SourceFile:2505)
05-07 22:51:08.616: E/Gmail(326):   at com.google.android.gm.ComposeActivityGmail.onOptionsItemSelected(SourceFile:342)
05-07 22:51:08.616: E/Gmail(326):   at android.app.Activity.onMenuItemSelected(Activity.java:2502)
05-07 22:51:08.616: E/Gmail(326):   at android.support.v4.app.l.onMenuItemSelected(SourceFile:350)
05-07 22:51:08.616: E/Gmail(326):   at android.support.v7.app.g.onMenuItemSelected(SourceFile:155)
05-07 22:51:08.616: E/Gmail(326):   at android.support.v7.app.i.a(SourceFile:74)
05-07 22:51:08.616: E/Gmail(326):   at android.support.v7.app.ActionBarActivityDelegateBase.a(SourceFile:556)
05-07 22:51:08.616: E/Gmail(326):   at android.support.v7.internal.view.menu.i.a(SourceFile:802)
05-07 22:51:08.616: E/Gmail(326):   at android.support.v7.internal.view.menu.m.b(SourceFile:153)
05-07 22:51:08.616: E/Gmail(326):   at android.support.v7.internal.view.menu.i.a(SourceFile:949)
05-07 22:51:08.616: E/Gmail(326):   at android.support.v7.internal.view.menu.i.a(SourceFile:939)
05-07 22:51:08.616: E/Gmail(326):   at android.support.v7.widget.ActionMenuView.a(SourceFile:596)
05-07 22:51:08.616: E/Gmail(326):   at android.support.v7.internal.view.menu.ActionMenuItemView.onClick(SourceFile:145)
05-07 22:51:08.616: E/Gmail(326):   at android.view.View.performClick(View.java:3574)
05-07 22:51:08.616: E/Gmail(326):   at android.view.View$PerformClick.run(View.java:14293)
05-07 22:51:08.616: E/Gmail(326):   at android.os.Handler.handleCallback(Handler.java:605)
05-07 22:51:08.616: E/Gmail(326):   at android.os.Handler.dispatchMessage(Handler.java:92)
05-07 22:51:08.616: E/Gmail(326):   at android.os.Looper.loop(Looper.java:137)
05-07 22:51:08.616: E/Gmail(326):   at android.app.ActivityThread.main(ActivityThread.java:4448)
05-07 22:51:08.616: E/Gmail(326):   at java.lang.reflect.Method.invokeNative(Native Method)
05-07 22:51:08.616: E/Gmail(326):   at java.lang.reflect.Method.invoke(Method.java:511)
05-07 22:51:08.616: E/Gmail(326):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:823)
05-07 22:51:08.616: E/Gmail(326):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:590)
05-07 22:51:08.616: E/Gmail(326):   at dalvik.system.NativeStart.main(Native Method)

在这里,我得到 FileNotFoundException异常 erroe。但是我对这个位置的PDF。(的/ mnt / SD卡/ MyMenu菜单/ 2015-05-07 22-50-39-Report.pdf

Here I am getting FileNotFoundException erroe. But I have pdf in this location.(/mnt/sdcard/MyMenu/2015-05-07 22-50-39-Report.pdf)

请让我知道我做了错误的。

Please let me know where I did mistake.

推荐答案

所以芬利我得到了(由@CommonsWare给出提示)解决方案。

So Finlay I got the solution(Tips given by @CommonsWare)..

sendMail(Uri.fromFile(emailFilePath));

发送电子邮件方法: -

Send email Method:-

private void sendMail(Uri URI) {
        try {

            String email = emailFromDB;

            String subject = "Report For the day " + getDateTime();

            String message = "Report For the day " + getDateTime();

            final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
            emailIntent.setType("application/pdf");
            emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL,new String[] { email });
            emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,subject);

            if (URI != null) {
                emailIntent.putExtra(Intent.EXTRA_STREAM, URI);
            }

            emailIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            this.startActivity(Intent.createChooser(emailIntent,"Sending email..."));

        } catch (Throwable t) {

            Toast.makeText(this,

            "Request failed try again: " + t.toString(),

            Toast.LENGTH_LONG).show();

        }

    }

在这里,我已删除 emailIntent.putExtra(android.content.Intent.EXTRA_TEXT,消息); 并获得URI像 Uri.fromFile( emailFilePath)。这解决了我的问题。由于@CommonnsWare .. !!!

Here I have removed emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, message); and get the URI like Uri.fromFile(emailFilePath). This solved my problem. Thanks to @CommonnsWare..!!!

这篇关于不能将PDF到邮件机器人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆