出口的android为CSV和发送的电子邮件附件 [英] android exporting to csv and sending as email attachment

查看:334
本文介绍了出口的android为CSV和发送的电子邮件附件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到多个线程在这个网站讨论有关发送邮件带有附件的机器人。我想尽一切方法讨论<一href="http://stackoverflow.com/questions/1247983/problem-sending-an-email-with-an-attachment-programmatically">here ,<一个href="http://stackoverflow.com/questions/4149265/sending-an-email-with-an-attachment-from-an-application">here和<一href="http://stackoverflow.com/questions/2264622/android-multiple-email-attachment-using-intent-question">here.

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.

我创建通过code一个CSV文件,此文件保存到Android内部存储。然后我想这个文件发送作为附件的电子邮件。那么,邮件被发送,我得到它没有依附。这是我做了什么。

I am creating a csv file via code and save this file to android internal storage. Then I want to send this file as attachment as 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);

我试图改变MIME设置为text / html的和文字/富文本等,但没有运气。谁能告诉我什么,我做错了什么?

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?

推荐答案

感谢大家谁试图help..After服用一个完整的一天,我从我与attachment..This应用程序发送电子邮件是工作code.。

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

String columnString =   "\"PersonName\",\"Gender\",\"Street1\",\"postOffice\",\"Age\"";
String dataString   =   "\"" + currentUser.userName +"\",\"" + currentUser.gender + "\",\"" + currentUser.street1 + "\",\"" + currentUser.postOFfice.toString()+ "\",\"" + currentUser.age.toString() + "\"";
String combinedString = columnString + "\n" + dataString;

File file   = null;
File root   = Environment.getExternalStorageDirectory();
if (root.canWrite()){
    File dir    =   new File (root.getAbsolutePath() + "/PersonData");
     dir.mkdirs();
     file   =   new File(dir, "Data.csv");
     FileOutputStream out   =   null;
    try {
        out = new FileOutputStream(file);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        try {
            out.write(combinedString.getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
Uri u1  =   null;
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/html");
startActivity(sendIntent);

此外,如果你已经安装你的手机的SD卡在机器中,该code不会工作。只有一个能够在同一时间访问的SD卡。因此,在这种情况下,卸载从计算机和try..Thanks你的SD卡,以谁回答<一个家伙href="http://stackoverflow.com/questions/2623516/cannot-write-to-sd-card-canwrite-is-returning-false/2623546#2623546">here..Also请确保你买的权限写入到外部存储在你的manifest文件...

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

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>

希望它可以帮助别人......感谢大家谁试图帮助..

Hope it helps someone...Thanks for everyone who tried to help..

这篇关于出口的android为CSV和发送的电子邮件附件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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