2013-01-21 36 views
1

我想生成Csv文件,并同时将它作为附件发送到电子邮件。它通过电子邮件发送时显示附件,但问题是当我检查收件箱时,我只发现没有附件的电子邮件。我会appriciate你的帮助。生成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; 
     } 
    } 
+0

你有任何这方面的喜悦?我有同样的问题! – PaulH

回答

0

可以看到代码,您GenerateCsv不能正常工作,你必须导入CSV库。在你的代码中没有任何。要下载检查:Csv library

,另请参阅:Read/Write csv

和清单权限:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
相关问题