android导出到csv并作为电子邮件附件发送
我在这个网站上看到了多个线程讨论如何在 Android 中发送带有附件的电子邮件。我尝试了此处讨论的所有方法,此处和此处。
我正在通过代码创建一个 csv 文件并将该文件保存到 android 内部存储中。然后我想将此文件作为电子邮件附件发送。嗯,电子邮件正在发送,我收到了,没有附件。这就是我所做的。
String columnString = "\"Person\",\"Gender\",\"Street1\",\"PostOfice\",\"Age\"";
String dataString = "\"" + currentUser.userName +"\",\"" + currentUser.gender + "\",\"" + currentUser.street1 + "\",\"" + currentUser.poNumber.toString() + "\",\"" + currentUser.age.toString() + "\"";
String combinedString = columnString + "\n" + dataString;
File file = new File(this.getCacheDir()+ File.separator + "Data.csv");
try {
FileOutputStream out = new FileOutputStream(file);
out.write(combinedString.getBytes());
out.close();
} catch (IOException e) {
Log.e("BROKEN", "Could not write file " + e.getMessage());
}
Uri u1 = Uri.fromFile(file);
Intent sendIntent = new Intent(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_SUBJECT, "Person Details");
sendIntent.putExtra(Intent.EXTRA_STREAM, u1);
sendIntent.setType("text/richtext");
startActivity(sendIntent);
我尝试将 mime 设置更改为“text/html”和“text/richtext”等。但还没有运气。谁能告诉我我做错了什么?
I have seen multiple threads in this site discussing about sending email with attachments in android. I tried every methods discussed here, here and here.
I am creating a csv file via code and saving this file to android internal storage. Then I want to send this file as attachment in an email. Well, the email is being sent, I am getting it without attachment. This is what I have done.
String columnString = "\"Person\",\"Gender\",\"Street1\",\"PostOfice\",\"Age\"";
String dataString = "\"" + currentUser.userName +"\",\"" + currentUser.gender + "\",\"" + currentUser.street1 + "\",\"" + currentUser.poNumber.toString() + "\",\"" + currentUser.age.toString() + "\"";
String combinedString = columnString + "\n" + dataString;
File file = new File(this.getCacheDir()+ File.separator + "Data.csv");
try {
FileOutputStream out = new FileOutputStream(file);
out.write(combinedString.getBytes());
out.close();
} catch (IOException e) {
Log.e("BROKEN", "Could not write file " + e.getMessage());
}
Uri u1 = Uri.fromFile(file);
Intent sendIntent = new Intent(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_SUBJECT, "Person Details");
sendIntent.putExtra(Intent.EXTRA_STREAM, u1);
sendIntent.setType("text/richtext");
startActivity(sendIntent);
I tried changing mime settings to "text/html" and "text/richtext" etc. But no luck yet. Can anyone tell me what I am doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
感谢所有试图提供帮助的人。花了一整天的时间后,我从我的应用程序发送了一封带有附件的电子邮件。这是工作代码。
另外,如果您已将手机 SD 卡安装在机器中,则此代码将无法工作。一次只有一个人可以访问 SDCard。因此,在这种情况下,从计算机上卸载 SDCard 并尝试..感谢回答 此处..还要确保您已购买在清单文件中写入外部存储的权限...
希望它可以帮助某人...感谢所有试图提供帮助的人。 。
Thanks for everyone who tried to help..After taking a full day I have send an email from my app with attachment..This is the working code..
Also If you have mounted your phone SDCard in the machine , this code wont work. Only one can access SDCard at one time. So in that case unmount your SDCard from computer and try..Thanks to the guy who answered here..Also make sure you have bought permission to write to external Storage in your manifest file...
Hope it helps someone...Thanks for everyone who tried to help..
此代码将帮助您
在
AndroidManifest.xml
文件中添加以下行:This Code will help you out
Add this line in
AndroidManifest.xml
file:尝试
Try
对于内部存储文件,需要使文件可读:
shareFile.setReadable(true, false);
For internal storage files, you need to make the file readable:
shareFile.setReadable(true, false);
以下是邮件中附件 csv 文件的代码(其工作代码):
MyCsvFile.csv”应该存在于您手机的内部/外部存储器中。
有关更多信息,请查看:https://stackoverflow.com/ a/48643905/8448886
以下是将 csv 文件附加到邮件中的代码:
Here is the code for attachment of csv file in mail (Its working code):
MyCsvFile.csv" should be present in your Internal/External memory of phone.
For More Look into this :https://stackoverflow.com/a/48643905/8448886
Below is the code for attachment of csv file into mail :