如何在文本文件中保存文本并通过Android中的电子邮件将其作为附件发送?

时间:2015-07-08 04:57:49

标签: android android-intent android-file

当我的用户想给我发电子邮件时,我想添加一个选项,他们可以从应用程序向我发送日志。我想在文本文件中保存一些文本,然后将该文本文件作为附件发送给用户将发送给我的电子邮件。

我尝试了以下两种方法。

方法#1

按如下方式调用以下两个函数:

            copyFileToExternal("test_file_name" + ".xml");
            sendEmail("nilashis@gmail.com", "test_file_name");

以下是功能:

private  File copyFileToExternal(String fileName) {
    File file = null;
    String newPath = Environment.getExternalStorageDirectory()+"/folderName/";
    try {
        File f = new File(newPath);
        f.mkdirs();
        FileInputStream fin = openFileInput(fileName);
        FileOutputStream fos = new FileOutputStream(newPath + fileName);
        //byte[] buffer = new byte[1024];
        byte[] buffer = "safdsdfsdfsdfsdfdsf".getBytes();
        int len1 = 0;
        while ((len1 = fin.read(buffer)) != -1) {
            fos.write(buffer, 0, len1);
        }
        fin.close();
        fos.close();
        file = new File(newPath + fileName);
        if (file.exists())
            return file;
    } catch (Exception e) {

    }
    return null;
}

//Method to email
private void sendEmail(String email, String fileName) {

    File file = new File(Environment.getExternalStorageState()+"/folderName/" + fileName+ ".xml");
    Uri path = Uri.fromFile(file);
    Intent intent = new Intent(android.content.Intent.ACTION_SEND);
    intent.setType("application/octet-stream");
    intent.putExtra(android.content.Intent.EXTRA_SUBJECT, "This is the subject I want");
    String to[] = { email };
    intent.putExtra(Intent.EXTRA_EMAIL, to);
    intent.putExtra(Intent.EXTRA_TEXT, "here is the message I want");
    intent.putExtra(Intent.EXTRA_STREAM, path);
    startActivityForResult(Intent.createChooser(intent, "Send mail..."),
            1222);
}

方法#2

不起作用:

public void doSendFile() {
    String xmlFilename = "fileToSend.txt";
    FileOutputStream fos = null;
    try {
        fos = openFileOutput(xmlFilename, MODE_WORLD_READABLE);

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    try {
        fos.write("this is test being written to ".getBytes());
    } catch (IOException e) {
        e.printStackTrace();
    }
    try {
        fos.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    Intent intent = new Intent(android.content.Intent.ACTION_SEND);
    intent.setType("text/plain");

//        Uri uri = Uri.fromFile(new File(xmlFilename));
    Uri uri = Uri.fromFile(new File("/mnt/sdcard/../.."+getFilesDir()+"/"+xmlFilename));
    intent.putExtra(android.content.Intent.EXTRA_STREAM, uri);
    startActivity(Intent.createChooser(intent, "Send eMail..asdasd"));

}

3 个答案:

答案 0 :(得分:0)

我想你可以从这里得到一些东西。 Sending Email in Android using JavaMail API without using the default/built-in app
http://techblogon.com/send-email-from-an-android-application-programmatically/

并在清单中添加此内容。

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

答案 1 :(得分:0)

以下是示例代码:

private void share(String strEmail){
    Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
    emailIntent.setType("text/plain");
    emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{strEmail});
    emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,"Log");
    emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, "From My App");
    emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///mnt/sdcard/files/log.txt"));
    startActivity(Intent.createChooser(emailIntent, "Send mail..."));
}

其中URI字符串必须是日志文件的路径。

更新: 其他你需要检查的事情 1.清单文件中的Internet权限 2.如果您能够打开邮件而没有看到附件,则URI路径可能存在问题。

答案 2 :(得分:0)

以下是示例代码。 uris是您要在邮件中共享的文件的路径。

Intent shareIntent = new Intent(Intent.ACTION_SEND_MULTIPLE);
    shareIntent.setType("application/pdf");
    shareIntent.putExtra(Intent.EXTRA_EMAIL, new String[] { "sender_mail_id" });
    shareIntent.putExtra(Intent.EXTRA_SUBJECT, "Subject");                               
    shareIntent.putExtra(Intent.EXTRA_TEXT, "Text to be displayed as the content");
    shareIntent.putExtra(Intent.EXTRA_STREAM, uris);
    startActivity(shareIntent);