我正在尝试创建一个pdf并通过电子邮件发送。现在我已经成功完成了pdf生成。当我尝试将pdf附加到邮件并发送时,我收到错误。
我这样想:
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();
}
}
当我尝试发送时,我得到了这个错误。
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
错误。但我在这个位置有pdf。(/mnt/sdcard/MyMenu/2015-05-07 22-50-39-Report.pdf
)
请告诉我错误的地方。
答案 0 :(得分:2)
所以Finlay我得到了解决方案(由@CommonsWare提供的提示)..
sendMail(Uri.fromFile(emailFilePath));
发送电子邮件方法: -
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, message);
并获取了Uri.fromFile(emailFilePath)
之类的URI。这解决了我的问题。感谢@CommonnsWare .. !!!