-1

I want to send images via email in my android app. For which I'm using Android Native Camera app and Intents to use the respective service. I've used the following code:
Email is getting send but if I'm trying to add image the app gets crash.

public class Complaints extends AppCompatActivity {
    Button sendEmail;
    EditText to, subject, msg;
    Bitmap image;
    Button camera;
    File pic;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_complaints);
        to = (EditText) findViewById(R.id.et1);
        subject = (EditText) findViewById(R.id.et2);
        msg = (EditText) findViewById(R.id.et3);
        sendEmail = (Button) findViewById(R.id.s_Email);
        camera = (Button) findViewById(R.id.btn_img);
        camera.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent();
                intent.setType("image/*");
                intent.setAction(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

startActivityForResult(Intent.createChooser(intent,"Select Picture"));
            }
        });


        sendEmail.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String Emailid = to.getText().toString();
                String sub = subject.getText().toString();
                String message = msg.getText().toString();

                Intent email = new Intent(Intent.ACTION_SEND);
                email.putExtra(Intent.EXTRA_EMAIL, new String[]{Emailid});
                email.putExtra(Intent.EXTRA_SUBJECT, sub);
                email.putExtra(Intent.EXTRA_TEXT, message);
                email.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);//this will make such that when user returns to your app, your app is displayed, instead of the email app.
               email.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(pic));
               email.setType("message/rfc822");
             email.setType("image/jpeg");

                try {
                    startActivity(Intent.createChooser(email, "Message was Sent"));
                }
                catch (ActivityNotFoundException e) {
                    Toast t = Toast.makeText(Complaints.this, "There is No Emial Client installed ", Toast.LENGTH_SHORT);
                    t.setGravity(Gravity.CENTER, 0, 10);
                    t.show();
                }
            }
        });


    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 10) {
            image = (Bitmap) data.getExtras().get("Data");
            ImageView i = (ImageView) findViewById(R.id.img);
            i.setImageBitmap(image);
            try
            {
                File root= Environment.getExternalStorageDirectory();
                if(root.canWrite())
                {
                    pic=new File(root,"pic.jpeg");
                    FileOutputStream out=new FileOutputStream(pic);
                    image.compress(Bitmap.CompressFormat.JPEG,100,out);
                    out.flush();
                    out.close();
                }

            } catch (IOException e)
            {
                Log.e("BROKEN", "Could not write file " + e.getMessage());
            }
        }
    }

}
ankit
  • 13
  • 6
  • What s the message of error ? Do you have add in Manifest file ? For this line "File root= Environment.getExternalStorageDirectory()" – guillaume boutin Apr 30 '17 at 07:34
  • java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=10, result=-1, data=Intent { dat=content://com.android.providers.media.documents/document/image:18738 flg=0x1 }} to activity {com.example.ankit.haridwartrafficpolice/com.example.ankit.haridwartrafficpolice.Complaints}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.os.Bundle.get(java.lang.String)' on a null object reference – ankit Apr 30 '17 at 10:46
  • Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.os.Bundle.get(java.lang.String)' on a null object reference – ankit Apr 30 '17 at 10:49
  • The error is produce by this line : image = (Bitmap) data.getExtras().get("Data"); You need to use : Uri dataUri = data.getData(); Source : https://developer.android.com/training/basics/intents/result.html – guillaume boutin Apr 30 '17 at 11:53

2 Answers2

0
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.support.v4.app.NavUtils;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;


import android.content.ContentValues;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.net.Uri;

import android.provider.MediaStore.Images;
import android.provider.MediaStore.Images.Media;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;


 public class Complaints extends Activity {
  Button send;
  Bitmap thumbnail;
  File pic;
  EditText address, subject, emailtext;
  protected static final int CAMERA_PIC_REQUEST = 0;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_complaints);
    send=(Button) findViewById(R.id.emailsendbutton);
    address=(EditText) findViewById(R.id.emailaddress);
    subject=(EditText) findViewById(R.id.emailsubject);
    emailtext=(EditText) findViewById(R.id.emailtext);










    Button camera = (Button) findViewById(R.id.button1); 
    camera.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0){
            Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);  

        }
        });

        send.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0){

            Intent i = new Intent(Intent.ACTION_SEND);
            i.putExtra(Intent.EXTRA_EMAIL, new String[]{"dummy@email.com"});
            i.putExtra(Intent.EXTRA_SUBJECT,"dummy subject");
            //Log.d("URI@!@#!#!@##!", Uri.fromFile(pic).toString() + "   " + pic.exists());
            i.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(pic));

            i.setType("image/png");
            startActivity(Intent.createChooser(i,"Share this"));
        }
        });


} 
0

Getting The Image Path and converting path into Uri :

File photo = new File(Environment.getExternalStorageDirectory()+"/Android/data/"+getApplicationContext().getPackageName()+"/Fault", imagename+".png")
Uri imageuri = Uri.fromFile(photo); 

Sending through Email Intent :

Intent send_report = new Intent(Intent.ACTION_SEND);
                                    send_report.putExtra(Intent.EXTRA_EMAIL, new String[]{ email_emailid}); 
                                    send_report.putExtra(Intent.EXTRA_SUBJECT, email_subject);
                                    send_report.putExtra(Intent.EXTRA_STREAM, imageuri);
                                    send_report.putExtra(Intent.EXTRA_TEXT, email_body);  
                                    send_report.setType("text/plain");
                                    send_report.setType("image/png");
                                    startActivityForResult(Intent.createChooser(send_report, "Choose an Email client"), 77);
Haris ali
  • 773
  • 1
  • 13
  • 19
  • ava.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=10, result=-1, data=Intent { dat=content://com.android.providers.media.documents/document/image:18738 flg=0x1 }} to activity {com.example.ankit.haridwartrafficpolice/com.example.ankit.haridwartrafficpolice.Complaints}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.os.Bundle.get(java.lang.String)' on a null object referenc – ankit Apr 30 '17 at 11:06
  • Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.os.Bundle.get(java.lang.String)' on a null object reference – ankit Apr 30 '17 at 11:07