android导出到csv并作为电子邮件附件发送

我在这个网站上看到多个线程讨论有关在Android中使用附件发送电子邮件。 我尝试了这里和这里讨论的所有方法。

我正在通过代码创build一个csv文件,并将此文件保存到android内部存储。 然后我想发送这个文件作为附件在电子邮件中。 那么,电子邮件正在发送,我没有附件得到它。 这就是我所做的。

String columnString = "\"Person\",\"Gender\",\"Street1\",\"PostOfice\",\"Age\""; String dataString = "\"" + currentUser.userName +"\",\"" + currentUser.gender + "\",\"" + currentUser.street1 + "\",\"" + currentUser.poNumber.toString() + "\",\"" + currentUser.age.toString() + "\""; String combinedString = columnString + "\n" + dataString; File file = new File(this.getCacheDir()+ File.separator + "Data.csv"); try { FileOutputStream out = new FileOutputStream(file); out.write(combinedString.getBytes()); out.close(); } catch (IOException e) { Log.e("BROKEN", "Could not write file " + e.getMessage()); } Uri u1 = Uri.fromFile(file); Intent sendIntent = new Intent(Intent.ACTION_SEND); sendIntent.putExtra(Intent.EXTRA_SUBJECT, "Person Details"); sendIntent.putExtra(Intent.EXTRA_STREAM, u1); sendIntent.setType("text/richtext"); startActivity(sendIntent); 

我试图改变MIME设置为“文本/ HTML”和“文本/富文本”等,但没有运气呢。 谁能告诉我我做错了什么?

Solutions Collecting From Web of "android导出到csv并作为电子邮件附件发送"

感谢大家谁试图帮助..后,整整一天,我已经发送了一个电子邮件从我的应用程序附件..这是工作代码..

 String columnString = "\"PersonName\",\"Gender\",\"Street1\",\"postOffice\",\"Age\""; String dataString = "\"" + currentUser.userName +"\",\"" + currentUser.gender + "\",\"" + currentUser.street1 + "\",\"" + currentUser.postOFfice.toString()+ "\",\"" + currentUser.age.toString() + "\""; String combinedString = columnString + "\n" + dataString; File file = null; File root = Environment.getExternalStorageDirectory(); if (root.canWrite()){ File dir = new File (root.getAbsolutePath() + "/PersonData"); dir.mkdirs(); file = new File(dir, "Data.csv"); FileOutputStream out = null; try { out = new FileOutputStream(file); } catch (FileNotFoundException e) { e.printStackTrace(); } try { out.write(combinedString.getBytes()); } catch (IOException e) { e.printStackTrace(); } try { out.close(); } catch (IOException e) { e.printStackTrace(); } } } Uri u1 = null; u1 = Uri.fromFile(file); Intent sendIntent = new Intent(Intent.ACTION_SEND); sendIntent.putExtra(Intent.EXTRA_SUBJECT, "Person Details"); sendIntent.putExtra(Intent.EXTRA_STREAM, u1); sendIntent.setType("text/html"); startActivity(sendIntent); 

另外,如果你已经在机器上安装了你的手机的SDCard,这段代码不会工作。 一次只能访问一个SDCard。 所以在这种情况下,从计算机卸载您的SD卡,并尝试..谢谢在这里回答的家伙..也确保你已经购买了清单文件中写入到外部存储的权限…

 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission> 

希望它可以帮助别人…感谢所有试图帮助的人..

尝试

 sendIntent.setType("message/rfc822"); 

本守则将帮助你

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

AndroidManifest.xml文件中添加这一行:

  <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission 

对于内部存储文件,您需要使文件可读:

shareFile.setReadable(true,false);