attach pdf files to an email

0

I can not get Android N versions or higher when trying to gmail content to write me the email, add the attached file, I say, uri.fromFile without problems but with Uri.parse. He tells me he could not attach anything.

these are my permissions

 <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.CAMERA" />

and this is the code I use. I hope someone could help me.

 public void enviarprecontrato1() {
        

            String pdfFilecreado = String.valueOf(new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/Rocamer/Contratos/", "PreContrato " + Variables.getnombreaseguradocontrato() + ".pdf"));

            Uri uri;
            //Validación de acuerdo al OS.
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                uri = Uri.parse(pdfFilecreado);

            } else {
               uri = Uri.fromFile(new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/Rocamer/Contratos/",  "PreContrato " + Variables.getnombreaseguradocontrato() + ".pdf"));
            }
            

        //Instanciamos un Intent del tipo ACTION_SEND
        Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);

        //Aqui definimos la tipologia de datos del contenido dle Email en este caso text/html
        emailIntent.setType("text/html");

        // Indicamos con un Array de tipo String las direcciones de correo a las cuales enviar
        emailIntent.putExtra(Intent.EXTRA_EMAIL, new String[]{emaildestino1.getText().toString()});

        // Aqui definimos un email para enviar como copia
        emailIntent.putExtra(Intent.EXTRA_CC, new String[]{emailcopia1.getText().toString()});

        // Aqui definimos un titulo para el Email
        emailIntent.putExtra(android.content.Intent.EXTRA_TITLE, emailtitulo1.getText().toString());

        // Aqui definimos un Asunto para el Email
        emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, emailasunto1.getText().toString());

        // Aqui obtenemos la referencia al texto y lo pasamos al Email Intent
        emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, emailcuerpo1.getText().toString());

        // Colocamos el adjunto en el stream
        emailIntent.putExtra(Intent.EXTRA_STREAM, uri);

        //indicamos el tipo de dato
        emailIntent.setType("application/pdf");

        try {
            //Enviamos el Correo iniciando una nueva Activity con el emailIntent.
            startActivity(Intent.createChooser(emailIntent, "Seleccionar metodo de envio..."));
        } catch (android.content.ActivityNotFoundException ex) {
            Toast.makeText(generarpdfprecontrato.this, "No hay ninguna aplicación de correo electrónico instalada.", Toast.LENGTH_LONG).show();
        }
    }

I want to indicate that the code has not been written by me from scratch, I saw it on the Internet and fixed it to my needs and I already report a thank you to the person who uploaded it.

and the routes are fine since Uri.fromFile works perfectly.

    
asked by jechu85 24.12.2018 в 10:29
source

0 answers