2013-01-20 81 views
0

我已經嘗試了下面的附件發送電子郵件的代碼。它在嘗試發送時顯示附件,但問題是它只發送帶有主題和正文但沒有附件的電子郵件。我會感謝您的幫助發送csv文件作爲附件

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. 

請告訴我在哪裏我有沒有出問題。

回答

0

它可能是因爲您的Gmail應用程序沒有權限從您的私人數據文件夾讀取。嘗試將文件存儲在外部存儲器中。

相關問題