无法在android中发送包含电子邮件的.csv文件

时间:2013-08-24 05:20:58

标签: android csv mime-types

我要在邮件中附上“.csv”并发送。但面临接收方无法获得csv文件的问题。

我尝试过太多Mime类型

application / octet-stream,text /逗号分隔值,text / csv,application / csv,application / excel,application / vnd.ms-excel,application / vnd.msexcel < / p>

但文件未附加邮件。

下面是我用来发送邮件的代码

public boolean sendEmail() {
    boolean success = false;
    Intent intentSendMail = new Intent(Intent.ACTION_SEND);

    File mydir = getApplicationContext().getDir(Global.FOLDERNAME, Context.MODE_PRIVATE); 
    File fileWithinMyDir = new File(mydir, Global.FILENAME); 
    if (!fileWithinMyDir.exists() || !fileWithinMyDir.canRead()) {
        Toast.makeText(this, "Attachment Error", Toast.LENGTH_SHORT).show();
        success = false;
    } else {
        intentSendMail.setType("text/csv");

        intentSendMail.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(fileWithinMyDir));

        intentSendMail.putExtra(Intent.EXTRA_SUBJECT,
                "Subject");

        intentSendMail.putExtra(Intent.EXTRA_TEXT, "Sent from my phone.");

        startActivity(Intent.createChooser(intentSendMail, "E-mail"));

        success = true;
    }
    return success;
}

提前致谢..

3 个答案:

答案 0 :(得分:2)

试试如下:

String FILE = Environment.getExternalStorageDirectory() + File.separator
        + "Foldername";
   Intent sendIntent = new Intent(android.content.Intent.ACTION_SEND);
   sendIntent.setType("application/csv");
   sendIntent.putExtra(android.content.Intent.EXTRA_EMAIL, "");
   sendIntent.putExtra(Intent.EXTRA_SUBJECT, "");
   sendIntent.putExtra(Intent.EXTRA_TEXT, "");
   String temp_path = FILE + "/" + "Filename.csv";
   File F = new File(temp_path);
    Uri U = Uri.fromFile(F);
    sendIntent.putExtra(Intent.EXTRA_STREAM, U);
    startActivity(Intent.createChooser(sendIntent, "Send Mail"));

希望这会对你有所帮助。

答案 1 :(得分:1)

我得到了解决这个问题的方法。 以下是解决方案

public boolean sendEmail() {
    String destLocation = "";
    String FILE = Environment.getExternalStorageDirectory()+"";
    destLocation =  FILE + "/" + Global.FILENAME;


    boolean success = false;
    Intent intentSendMail = new Intent(Intent.ACTION_SEND);

    File mydir = getApplicationContext().getDir(Global.FOLDERNAME, Context.MODE_PRIVATE); 
    File fileWithinMyDir = new File(mydir, Global.FILENAME);

    copyFile(Uri.fromFile(fileWithinMyDir).toString(),destLocation);

    if (!fileWithinMyDir.exists() || !fileWithinMyDir.canRead()) {
        Toast.makeText(this, "Attachment Error", Toast.LENGTH_SHORT).show();
        success = false;
    } else {
        intentSendMail.setType("application/csv");
        intentSendMail.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://"+destLocation));

        intentSendMail.putExtra(Intent.EXTRA_SUBJECT,
                "Test Play file.");
        intentSendMail.putExtra(Intent.EXTRA_TEXT, "");

        startActivity(Intent.createChooser(intentSendMail, "E-mail"));

        success = true;
    }
    return success;
}

答案 2 :(得分:0)

试试这个

emailIntent.setType("plain/text");
emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///mnt/sdcard/sanket test/siteriskassesment.csv"));

OR

请参阅this