Good evening, it turns out that I have my application in which I generate a pdf, then want to send an email and attach the pdf file, the pdf file generates it well, but I do not know how to attach the file. Thanks in advance.
Good evening, it turns out that I have my application in which I generate a pdf, then want to send an email and attach the pdf file, the pdf file generates it well, but I do not know how to attach the file. Thanks in advance.
You have to get the URI of the file, so that:
Uri uri = Uri.fromFile(new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/CALC/REPORTS/",pdfname ));
An example:
String[] mailto = {""};
Uri uri = Uri.fromFile(new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/CALC/REPORTS/",pdfname ));
Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.putExtra(Intent.EXTRA_EMAIL, mailto);
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Calc PDF Report");
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT,"Hi PDF is attached in this mail. ");
emailIntent.setType("application/pdf");
emailIntent.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(emailIntent, "Send email using:"));
Update: Open folder:
private void showFile(Uri fileUri){
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(fileUri, "file/*");
startActivity(intent);
}
Also:
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("content://media/external/"))); /** Reemplazar con tu propio uri */
Attach several PDFs:
final Intent ei = new Intent(Intent.ACTION_SEND_MULTIPLE);
ei.setType("plain/text");
ei.putExtra(Intent.EXTRA_EMAIL, new String[] {"[email protected]"});
ei.putExtra(Intent.EXTRA_SUBJECT, "That one works");
And then you have to add the URIS:
ArrayList<Uri> uris = new ArrayList<Uri>();
ei.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
startActivityForResult(Intent.createChooser(ei, "Sending multiple attachment"), 12345);
Not only can you attach a .pdf, you can attach any type of files, in this case the type that would be determined by the MIME TYPE that for a .pdf file must be "application/pdf"
.
Attaching a file when sending can be done through an Intent where you define the Uri resource if it is only one or a list of Uri 's to send multiple files:
String archivoPDF = "archivo.pdf";
//Obtiene la Uri del recurso.
Uri uri = Uri.fromFile(new
File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/PDFS/", archivoPDF));
//Crea intent para enviar el email.
Intent i = new Intent(Intent.ACTION_SEND);
i.setType("application/pdf");
//Agrega email o emails de destinatario.
i.putExtra(Intent.EXTRA_EMAIL, new String[] { "[email protected]" });
i.putExtra(Intent.EXTRA_SUBJECT, "Envio de archivo PDF.");
i.putExtra(Intent.EXTRA_TEXT, "Hola te envío un archivo PDF.");
i.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(i, "Enviar e-mail mediante:"));
Your question refers:
How to attach files in android studio to send by email?
If you want to attach several files, you must create a List of Uri 's which will contain the paths of the files.
//Crear una Lista de Uris.
List<Uri> listUris = new ArrayList<Uri>();
//Agrega elementos (como ejemplo 3 elementos).
lstUris.add(Uri.fromFile(new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/PDFS/", "archivo1.pdf")););
lstUris.add(Uri.fromFile(new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/PDFS/", "archivo2.pdf")););
lstUris.add(Uri.fromFile(new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/PDFS/", "archivo3.pdf")););
//Crea intent para enviar el email.
Intent i = new Intent(Intent.ACTION_SEND);
i.setType("application/pdf");
//Agrega email o emails de destinatario.
i.putExtra(Intent.EXTRA_EMAIL, new String[] { "[email protected]" });
i.putExtra(Intent.EXTRA_SUBJECT, "Envio de archivos PDF.");
i.putExtra(Intent.EXTRA_TEXT, "Hola te envío archivos PDF.");
//Define listado de Uris de cada archivo.
i.putExtra(Intent.EXTRA_STREAM, listUris);
startActivity(Intent.createChooser(i, "Enviar e-mail mediante:"));