Question

I am trying to send my CSV file via e-mail in my app. When i click send it show the person i'm sending to, the subject, the message and the attached file, but when it sends the phone says "It wasnt possible to show the attached file". When i check my e-mail box, the message is there but without the file.

String to=destinatario.getText().toString().trim();
                String subj=subject.getText().toString().trim();
                String msg=message.getText().toString().trim();

                if(to.length() < 1)
                {
                    Toast.makeText(getApplicationContext(), "Mete para quem quer mandar", Toast.LENGTH_LONG).show();
                }
                else if (subj.length() < 1) {
                    Toast.makeText(getApplicationContext(), "Introduza o Tema", Toast.LENGTH_LONG).show();
                }
                else if (msg.length() < 1) {
                    Toast.makeText(getApplicationContext(), "Introduza Mensagem", Toast.LENGTH_LONG).show();
                }
                else {
                    Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND); 
                    //emailIntent.setType("image/jpeg");
                    emailIntent.setType("message/rfc822");
                    emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{to}); 
                    emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subj); 
                    emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, msg); 
                    emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(file.getAbsolutePath()));
                    startActivity(Intent.createChooser(emailIntent, "A enviar..."));
                }   
            }

Does anyone know why this happens??

Was it helpful?

Solution

Change your extra stream Uri.parse String to "file://" + file.getAbsolutePath().

This should work as long as your file is the correct file

OTHER TIPS

I use the following code to create an intent with a csv attachment.

ArrayList<Uri> uriList = new ArrayList<Uri>();
    ArrayList<String> fileNameList = new ArrayList<String>();
    uriList.add(Uri.fromFile(f));
    fileNameList.add(f.getName());

    final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND_MULTIPLE);
    emailIntent.setType("text/plain");
    emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL,
            new String[]{""});
    emailIntent.putExtra(android.content.Intent.EXTRA_CC,
            new String[]{""});
    emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Log");

    if (!uriList.isEmpty()) {
        emailIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uriList);
        emailIntent.putStringArrayListExtra(Intent.EXTRA_TEXT, fileNameList);
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top