문제

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??

도움이 되었습니까?

해결책

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

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

다른 팁

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);
    }
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top