Android exportar a csv y enviar como adjunto de correo electrónico

He visto varios hilos en este sitio discutiendo sobre el envío de correo electrónico con archivos adjuntos en android. He intentado todos los métodos discutidos aquí , aquí y aquí .

Estoy creando un archivo del csv vía código y excepto este archivo al almacenaje interno del android. Entonces quiero enviar este archivo como archivo adjunto en un correo electrónico. Bueno, el correo electrónico está siendo enviado, lo estoy recibiendo sin adjuntos. Esto es lo que he hecho.

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

Intenté cambiar la configuración de mime a "text / html" y "text / richtext" etc. Pero aún no hay suerte. ¿Puede alguien decirme lo que estoy haciendo mal?

Gracias por todos los que intentaron ayudar. Después de tomar un día entero he enviado un correo electrónico desde mi aplicación con archivo adjunto .. Este es el código de trabajo ..

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

También Si usted ha montado su SDCard del teléfono en la máquina, este código no funcionará. Sólo uno puede acceder a SDCard al mismo tiempo. Así que en ese caso desmontar su SDCard de la computadora y tratar .. Gracias al chico que respondió aquí .. También asegúrese de que ha comprado permiso para escribir en almacenamiento externo en su archivo de manifiesto …

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

Espero que ayude a alguien … Gracias por todos los que intentaron ayudar.

Tratar

 sendIntent.setType("message/rfc822"); 

Este Código le ayudará a salir

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

Añadir esta línea en el archivo AndroidManifest.xml :

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

Para archivos de almacenamiento interno, debe hacer que el archivo sea legible:

ShareFile.setReadable (true, false);

  • Sqlite base de datos en el idioma persa
  • Exportar mis datos en el archivo CSV desde la aplicación Android
  • La forma más rápida de leer un archivo CSV en Java
  • ¿Cómo puedo mostrar contenido CSV específico en el elemento de la lista que se ha hecho clic?
  • Importar contactos CSV en el teléfono Android 4
  • Cómo exportar datos a un archivo csv en Android?
  • Cómo implementar Exportar sqlite Para sobresalir / archivo csv en android?
  • Lectura de datos de 4 archivos CSV estáticos en Android: Best Approach?
  • Android: ¿Cómo escribir una nueva línea en un archivo CSV?
  • Leer CSV línea por línea en Kotlin
  • Archivo JSON VS SQLite android
  • FlipAndroid es un fan de Google para Android, Todo sobre Android Phones, Android Wear, Android Dev y Aplicaciones para Android Aplicaciones.