Can not attach pdf into mail in android

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.

I am trying like this:

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();

        }

    }

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)

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.

Upvotes: 1

Views: 1991

Answers (1)

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();

        }

    }

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..!!!

Upvotes: 2

Related Questions