我在 android 上工作,并尝试发送一封带有附件的电子邮件,但出现以下错误。
android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.SENDTO dat=file://assets/Cards/myfile.pdf typ=Applications/pdf flg=0x10000000 (has extras) }
我是 android 开发的新手,所以我有点不知道我需要做什么。以下是我目前正在尝试的
Intent intent = new Intent(Intent.ACTION_SENDTO ); // it's not ACTION_SEND
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_SUBJECT, "Card Set "));
intent.putExtra(Intent.EXTRA_TEXT, "");
intent.setData(Uri.parse("mailto:"));
intent.setDataAndType(Uri.parse("file://assets/Cards/" + "myfile.pdf"),
"Applications/pdf");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
activity.startActivity(intent);
我可以在不附加文件的情况下正常创建和发送电子邮件。任何关于我应该如何正确附加文件的帮助都将非常感谢
最佳答案
好的,感谢 Kartik 的链接,我设法找到了附加文件的正确方式(这是一种有效的方式)。 四处搜索后,我发现这是为了展示如何将文件存储到外部存储器 http://developer.android.com/reference/android/os/Environment.html#getExternalStoragePublicDirectory%28java.lang.String%29
所以我使用此页面上的修改后的 createExternalStoragePublicPicture() 写入内存,然后执行以下操作
createExternalStoragePublicPicture();
File path = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES);
File file = new File(path, "cards_01.pdf");
Intent intent = new Intent(Intent.ACTION_SEND ,Uri.parse("mailto:")); // it's not ACTION_SEND
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_SUBJECT, "Card Set ");
intent.putExtra(Intent.EXTRA_TEXT, "");
intent.putExtra(Intent.EXTRA_STREAM,Uri.fromFile(file));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // this will make such that when user returns to your app, your app is displayed, instead of the email app.
activity.startActivity(intent);
上面的链接还显示了如何删除文件,并说明了如何将它们放在正确的文件夹中以避免覆盖其他文件。 希望这可以帮助其他有同样问题的人
关于java - 将 PDF 文件附加到来自应用程序的电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12405892/