Android Email Attachment Dynamic with file extension

时间:2011-11-02 09:23:12

标签: android file email attachment dynamic

如何附加内存中的文件,例如字符串,文件扩展名为.txt等。我已经尝试将字符串存储到内部存储器然后附加它我无法使其工作要么我更愿意解决我的第一个问题。

    Intent i = new Intent(Intent.ACTION_SEND);
    i.setType("plain/text");

    try {
        FileOutputStream fos = openFileOutput("data.enw", Context.MODE_PRIVATE);
        fos.write(export_text.getBytes());
        fos.close();
    } catch (Exception e) {
        e.printStackTrace();
    }

    i.putExtra(Intent.EXTRA_STREAM, Uri.parse(new File("data.enw").toString()));
    i.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{"MyEmail"});
    i.putExtra(android.content.Intent.EXTRA_SUBJECT, "Attachment");
    i.putExtra(android.content.Intent.EXTRA_TEXT, "Import the attachment");
    startActivity(Intent.createChooser(i, "E-mail"));

[编辑]下面的代码有效但需要SD卡。是否有更好的方法。

Intent i = new Intent(Intent.ACTION_SEND);
i.setType("plain/text");

File data = null;

try {
data = File.createTempFile("data", ".enw");
FileOutputStream out = new FileOutputStream(data);
out.write(export_text.getBytes());
out.close();

} catch (IOException e) {
e.printStackTrace();
}

i.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(data));
i.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{"MyEmail"});
i.putExtra(android.content.Intent.EXTRA_SUBJECT, "Attachment");
i.putExtra(android.content.Intent.EXTRA_TEXT, "Import the attachment");
startActivity(Intent.createChooser(i, "E-mail"));

0 个答案:

没有答案