2

我需要开发一个小型电子邮件应用程序,我必须生成 CSV 并使用 android 发送该 CSV 文档。我必须发送此附件而不保存到存储中。我发现使用文件编写器和我的代码附加 CSV 文件很困难,如下所示:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    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 email = new Intent(Intent.ACTION_SEND);
          email.putExtra(Intent.EXTRA_EMAIL, new String[]{ to});
          //email.putExtra(Intent.EXTRA_CC, new String[]{ to});
          //email.putExtra(Intent.EXTRA_BCC, new String[]{to});
          email.setType("text/csv");
          email.putExtra(Intent.EXTRA_SUBJECT, subject);
          email.putExtra(Intent.EXTRA_TEXT, message);
        email.putExtra(Intent.EXTRA_STREAM,generateCsvFile("testdata.csv"));

          //need this to prompts email client only

          startActivity(Intent.createChooser(email, "Choose an Email client :"));

        }
    });
}
public static FileWriter generateCsvFile(String sFileName) throws IOException
   {
       FileWriter writer;
       writer = new FileWriter(sFileName);
       writer.append("DisplayName");
       writer.append(',');
       writer.append("Age");
       writer.append('\n');

       writer.append("RajeshV");
       writer.append(',');
       writer.append("26");
       writer.append('\n');

       writer.append("Mrithula");
       writer.append(',');
       writer.append("29");
       writer.append('\n');

      return writer;
    }

}

我在这一行遇到了问题:

email.putExtra(Intent.EXTRA_STREAM,generateCsvFile("testdata.csv"));
4

5 回答 5

2

以下是发送附件的步骤

Intent email = new Intent(Intent.ACTION_SEND);

email .setType("image/jpeg");

email .putExtra(Intent.EXTRA_SUBJECT, "My Picture");

email .putExtra(Intent.EXTRA_STREAM, Uri.parse("file://sdcard/captureimage.png"));

startActivity(Intent.createChooser(email , "Email:"));

有关更多信息,您可以参考以下链接

多个附件

带有附件的电子邮件

于 2012-07-13T09:15:25.153 回答
2

我得到了工作代码

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    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;
    }
}

Add this line in AndroidManifest.xml file:

  <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-p
于 2012-07-14T07:44:34.047 回答
1
@Override
public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        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 email = new Intent(Intent.ACTION_SEND);
              email.putExtra(Intent.EXTRA_EMAIL, new String[]{ to});
              //email.putExtra(Intent.EXTRA_CC, new String[]{ to});
              //email.putExtra(Intent.EXTRA_BCC, new String[]{to});
              email.putExtra(Intent.EXTRA_SUBJECT, subject);
              email.putExtra(Intent.EXTRA_TEXT, message);

              //need this to prompts email client only
              email.setType("message/rfc822");

              startActivity(Intent.createChooser(email, "Choose an Email client :"));

            }
        });
    }
}
于 2012-07-14T07:31:26.613 回答
1

我已经完成了从 SD 卡发送带有邮件附件的任何文件..

Intent sendEmail= new Intent(Intent.ACTION_SEND);
   sendEmail.setType("rar/image");
   sendEmail.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new        
             File("/mnt/sdcard/download/abc.rar")));
             startActivity(Intent.createChooser(sendEmail, "Email:"));
于 2012-11-21T15:23:08.247 回答
0
Intent jj=new Intent(android.content.Intent.ACTION_SEND);

Intent jj=new Intent(android.content.Intent.ACTION_SEND_MULTIPLE);

String fileName = "file://" + Environment.getExternalStorageDirectory()+"/"+filename;         

jj.putExtra(Intent.EXTRA_SUBJECT, "Chat Info");

jj.putExtra(Intent.EXTRA_STREAM, Uri.parse(fileName));

jj.setType("message/rsc822");

Intent chooser = Intent.createChooser(jj , "Select Sender");

context.startActivity(chooser);
于 2014-07-25T07:36:03.830 回答