Attach File to Gmail via Intent

Asked

Viewed 518 times

2

I have a problem... When I try to open Gmail via Intent to attach a file, it pops up the part of writing a new email, but it doesn’t attach my file. The PDF is inside a folder of the app itself with this path.:(/Storage/Emulated/0/Android/data/teste.com.br.cartaovisitateste/files/business.pdf) but he doesn’t take the PDF from inside to attach in the email Does anyone know what it might be? My Gmail Intent code:

Intent i = new Intent(Intent.ACTION_SEND);
i.setType("vnd.android.cursor.dir/email");
i.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
i.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
i.putExtra(Intent.EXTRA_EMAIL, "");
//  funciona parcialmente, mas ele diz que encontrou um arquivo vazio
i.putExtra(Intent.EXTRA_STREAM, Uri.parse("/storage/emulated/0/Android/data/teste.com.br.cartaovisitateste/files/business.pdf"));
//  it does not work too
Uri.fromFile(new File(Environment.getDataDirectory().getAbsolutePath(), "business.pdf"));
i.putExtra(Intent.EXTRA_SUBJECT, String.valueOf(Hawk.get("registro_nome")).concat(" Business Card"));
startActivity(Intent.createChooser(i, "Enviando e-mail..."));
  • I don’t know what kind of application you are developing, but probably the person will have an interface to select the file, to then be sent. So I advise you to already get the address of the file through the method "onActivityResult", that address you static put various very depending on the android version. If your code presented an error, add along with your post, for a better understanding of the problem.

  • A priori, for testing, my application is working this way: 1º - I have a lib that generates a PDF file picking up a layout that I have 2º - I already open the application, and it internally generates the screen PDF 3º - I have a button to share by e-mail, and then I would click there and would share by email Actually I don’t have an application, it does everything without a screen

1 answer

1


Do it that way:

File f = new File("/storage/emulated/0/Android/data/teste.com.br.cartaovisitateste/files/business.pdf");
i.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(f));

Browser other questions tagged

You are not signed in. Login or sign up in order to post.