How to send the picture from imageview to gmail?

Viewed 86

Hi I am trying to send the picture from imageview to gmail. I am able to capture image from camera and set it on ImageView as well as retrieve image from gallery but not able to send the picture to other applications like gmail.

This is the question that I need to solve

Create new Activity:

Add 4 buttons camera, gallery, share camera image, share gallery image and 2 imageviews. When user click on camera it will select image from camera and set the imageview. When user clicks on gallery image, it selects the image from gallery and set the imageview. When user click on share button it will share your selected image to any application like gmail, Facebook etc.

here is my xml code

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".Camera2">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Camera"
        android:id="@+id/btn_cam"
        />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/_10sdp"
        android:text="Gallery"
        android:id="@+id/btn_gal"
        />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/_10sdp"
        android:text="Share Camera Image"
        android:id="@+id/btn_share_cam_img"
        />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Share Gallery Image"
        android:id="@+id/btn_share_gal_img"
        android:layout_marginTop="@dimen/_10sdp"
        />

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/iv1"
        android:layout_marginTop="@dimen/_100sdp"
        />

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/iv2"
        android:layout_marginTop="@dimen/_100sdp"
        />

</LinearLayout>

Here is my java code

package com.testing;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.core.content.FileProvider;

import android.Manifest;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Camera2 extends AppCompatActivity {

    Button btn_camera;
    Button btn_gal;
    ImageView iv1;
    Button btn_share_cam_img;
    Uri URI = null;

    ImageView iv2;
    Integer REQUEST_CAMERA=1, SELECT_FILE=0;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_camera2);

        btn_camera=findViewById(R.id.btn_cam);
        btn_gal=findViewById(R.id.btn_gal);
        iv1=findViewById(R.id.iv1);
           iv2=findViewById(R.id.iv2);
btn_share_cam_img=findViewById(R.id.btn_share_cam_img);



        btn_camera.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

           // taking photo from mobile and sharing it to other apps(will be done later its example of implicit intent)
//
                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

                startActivityForResult(intent, REQUEST_CAMERA);


            }
        });

        btn_gal.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                intent.setType("image/*");
                //startActivityForResult(intent.createChooser(intent, "Select File"), SELECT_FILE);
                startActivityForResult(intent, SELECT_FILE);
            }
        });

        btn_share_cam_img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                File image = new File(pictureDir, FILE_NAME);
//                fileUri = Uri.fromFile(image);
//                iv1.setImageURI(fileUri);
                emailPicture();
            }
        });
    }



    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);


        if (resultCode == Activity.RESULT_OK) {

            if (requestCode == REQUEST_CAMERA) {



                Bundle bundle = data.getExtras();
                final Bitmap bmp = (Bitmap) bundle.get("data");
                iv1.setImageBitmap(bmp);




            } else if (requestCode == SELECT_FILE) {

                Uri selectedImageUri = data.getData();
                iv2.setImageURI(selectedImageUri);
            }

        }
    }



    public void emailPicture()
    {
        Toast.makeText(
                this,
                "Now, sending the mail",
                Toast.LENGTH_LONG)
                .show();

        Intent emailIntent
                = new Intent(
                android.content.Intent.ACTION_SEND);
        emailIntent.setType("application/image");




        emailIntent.putExtra(
                android.content.Intent.EXTRA_EMAIL,
                new String[] {

                        // default receiver id
                        "ron34@gmail.com" });

        // Subject of the mail
        emailIntent.putExtra(
                android.content.Intent.EXTRA_SUBJECT,
                "New photo");

        // Body of the mail
        emailIntent.putExtra(
                android.content.Intent.EXTRA_TEXT,
                "Here's a captured image");

        // Set the location of the image file
        // to be added as an attachment


        

        // Start the email activity
        // to with the prefilled information
        startActivity(
                Intent.createChooser(emailIntent,
                        "Send mail..."));
    }






    }
1 Answers

I can't see where you're adding the image to be sent with the intent.

For supplying a file with the intent, you use Intent.EXTRA_STREAM.

As:

//This imageUri is the path of your image that you want to share
//This works with other applications like WhatsApp as well 
emailIntent.putExtra(Intent.EXTRA_STREAM, imageUri);
emailIntent.setType("image/jpeg");

You can also add a flag for Read URI permission to avoid no permission exception:

emailIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
Related