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.
- Importar archivo .csv a Sqlite en Android
- Recuperar datos de Sqlite y almacenarlos en un archivo CSV
- Datos de exportación de Android a csv
- Android no crea archivo
- Android java regex coinciden con todos menos un carácter
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?
- Cómo obtener la ruta absoluta en Android para el archivo?
- Cómo convertir los registros de base de datos en archivo csv en android?
- Exportar la base de datos SQLite al archivo csv en android
- ¿Correcta manera de agregar una nueva carpeta de recursos en Android Studio?
- Obtener y analizar archivo CSV en android
- ¿Podemos escribir datos GPS en un archivo CSV cuando obtenemos datos GPS en Android?
- ¿Cuántas filas puedo insertar en una tabla en SQLite Android?
- ¿Cómo leer el archivo csv en android?
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);
- Error de Android: java.lang.IllegalStateException: intentando repetir la consulta de un cursor ya cerrado
- ¿Cómo agregar android a la plataforma de Córdova? PATH error?