将csv文件作为附件发送

时间:2013-01-20 22:19:07

标签: android android-intent email-attachments

我尝试过以下代码发送带附件的电子邮件。它确实在尝试发送时显示附件,但问题是它只发送带有主题和正文的电子邮件但没有附件。我将非常感谢您的帮助

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

 textTo = (EditText) findViewById(R.id.txtenterserial);
 textSubject = (EditText) findViewById(R.id.txtenterseri);
 textMessage = (EditText) findViewById(R.id.txtenters);

 emailButton.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("text/plain");
         File data = null;
         try {
             Date dateVal = new Date();
             String filename = dateVal.toString();
             data = File.createTempFile("Report", ".csv");
             Log.d("vvvvvv", data.toString());
              FileWriter out = (FileWriter) GenerateCsv.generateCsvFile(data, "Name,Data1");
             //Log.d("SSSSHOOOOW", out.toString());
             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 static  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;
     }
 }

这是LogCat

01-20 22:07:26.998: E/Trace(8471): error opening trace file: No such file or directory (2)
01-20 22:07:27.929: D/gralloc_goldfish(8471): Emulator without GPU emulation detected.
01-20 22:07:46.538: D/vvvvvv(8471): /data/data/com.example.emailandroid/cache/Report1916903470.csv
01-20 22:07:46.998: D/dalvikvm(8471): GC_CONCURRENT freed 201K, 5% free 4318K/4544K, paused 78ms+82ms, total 292ms
01-20 22:07:47.458: I/Choreographer(8471): Skipped 94 frames!  The application may be doing too much work on its main thread.
01-20 22:07:51.828: W/IInputConnectionWrapper(8471): showStatusIcon on inactive InputConnection
01-20 22:07:52.718: I/Choreographer(8471): Skipped 58 frames!  The application may be doing too much work on its main thread.
01-20 22:09:40.928: I/Choreographer(8471): Skipped 33 frames!  The application may be doing too much work on its main thread.

请告诉我哪里出错了。

1 个答案:

答案 0 :(得分:0)

这可能是因为你的gmail应用程序没有权限从你的私人数据文件夹中读取。尝试将文件存储在外部存储器中。