1

我正在尝试生成 Csv 文件,同时将其作为电子邮件附件发送。它在通过电子邮件发送时显示附件,但问题是当我检查收件箱时,我只找到没有附件的电子邮件。我会感谢你的帮助。

这是我尝试过的代码

 buttonSend = (Button) findViewById(R.id.buttonSend);

        textTo = (EditText) findViewById(R.id.editTextTo);
        textSubject = (EditText) findViewById(R.id.editTextSubject);
        textMessage = (EditText) findViewById(R.id.editTextMessage);

        buttonSend.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                String to = textTo.getText().toString();
                String subject = textSubject.getText().toString();
                String message = textMessage.getText().toString();

                Intent i = new Intent(Intent.ACTION_SEND);
                i.setType("plain/text");
                File data = null;
                try {
                    Date dateVal = new Date();
                    String filename = dateVal.toString();
                    data = File.createTempFile("Report", ".csv");
                    FileWriter out = (FileWriter) GenerateCsv.generateCsvFile(
                            data, "Name,Data1");
                    i.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(data));
                    i.putExtra(Intent.EXTRA_EMAIL, new String[] { to });
                    i.putExtra(Intent.EXTRA_SUBJECT, subject);
                    i.putExtra(Intent.EXTRA_TEXT, message);
                    startActivity(Intent.createChooser(i, "E-mail"));

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

            }
        });
    }

    public class GenerateCsv {
        public static FileWriter generateCsvFile(File sFileName,String fileContent) {
            FileWriter writer = null;

            try {
                writer = new FileWriter(sFileName);
                writer.append(fileContent);
                             writer.flush();

            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }finally
            {
                try {
                    writer.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            return writer;
        }
    }
4

1 回答 1

1

可以看出,您的 GenerateCsv 不起作用,您必须导入 csv 库。在您的代码中没有任何内容。要下载检查这个:Csv 库

另请参阅:读/写 csv

和清单中的许可:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
于 2013-12-24T08:07:39.033 回答