Attach a photo to the email

Asked

Viewed 398 times

3

Good morning to all, the doubt is as follows. I have some basic fields (name, phone etc), and an option to take pictures. I have already working sending email and the button to take photo, I can get the values of all fields and take the photo including, but I want to know how I do to attach the photo in this email...

These are the methods to take the photo and save it on the mobile memory card.

public void abrirCamera(){
        File picsDir =       Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
        File imageFile = new File(picsDir, "foto.jpg");
        Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        i.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(imageFile));
        startActivity(i);
    }

public void takePhoto(View view)
{
    Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
    File photo = new File(Environment.getExternalStoragePublicDirectory
            (Environment.DIRECTORY_PICTURES), "Pic.jpg");

    intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo));
    imageUri = Uri.fromFile(photo);

    startActivityForResult(intent, TAKE_PICTURE);
}

public void onActivityResult(int requestCode, int resultCode, Intent data) {

    super.onActivityResult(requestCode, resultCode, data);

    switch (requestCode) {

        case TAKE_PICTURE:
            if (resultCode == Activity.RESULT_OK) {

                Uri selectedImage = imageUri;
                getActivity().getContentResolver().notifyChange(selectedImage, null);

                ImageView imageView = (ImageView)  getActivity().findViewById(R.id.ImageView);

                ContentResolver cr = getActivity().getContentResolver();
                Bitmap bitmap;


                try {
                    bitmap = android.provider.MediaStore.Images.Media.getBitmap(cr, selectedImage);

                    imageView.setImageBitmap(bitmap);
                    fotoCart.setText("");
                    //Toast.makeText(getActivity(), selectedImage.toString(), Toast.LENGTH_LONG).show();

                } catch (Exception e) {
                    //Toast.makeText(getActivity(), "Failed to load", Toast.LENGTH_SHORT).show();
                    Log.e("Camera", e.toString());
                }
            }
    }
  • You can add some code snippets to help us evolve it?

  • These methods are linked to another class called Camera. But I think it is possible to get the photo without having to access this other class...

  • What about Javamail? I have an application I made with Javamail!

1 answer

1


You’re gonna need a Intent putting the Uri of your photo as EXTRA_STREAM.

Try something similar:

private void enviaEmail(Uri uriDaSuaFoto) {
    Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND); 

    //Definindo que o conteúdo sera uma imagem
    emailIntent.setType("application/image");

    //Qual e-mail a ser enviado
    emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, "[email protected]"); 

    //Titulo do e-mail
    emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,"Titulo"); 

    //Corpo do e-mail
    emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, "Corpo do email"); 

    //Aqui você coloca a uri da sua imagem
    emailIntent.putExtra(Intent.EXTRA_STREAM, uriDaSuaFoto);

    //Criando um dialog para o usuário poder escolher qual aplicação enviar o e-mail
    startActivity(Intent.createChooser(emailIntent, "Enviar e-mail com"));
}
  • Man, thank you so much helped me so much... It all worked out, yeah.

Browser other questions tagged

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