Aalap Patel
Aalap Patel

Reputation: 2076

Send Image in message body of email android

My activity has an imageview, edittext with user entered email address and a send button. On send button activity I have email message, subject and recipient name ready but I also want to add the image which has been contained by imageview.

    Intent intent = new Intent(Intent.ACTION_SEND);
    intent.setData(Uri.parse("mailto:"));
    intent.setType("image/jpeg");
    intent.putExtra(Intent.EXTRA_EMAIL, receivers);
    intent.putExtra(Intent.EXTRA_SUBJECT, "My subject");
    intent.putExtra(Intent.EXTRA_TEXT, "hello wats up");
    intent.putExtra(Intent.EXTRA_STREAM, image i want to send);
    startActivity(intent); 

obviously the last putExtra line will give me error stating they want string but I am passing imageview. Guide me please how can I include my imageview in this email body. (Not as an attachment but in message body with message text).

Many thank you in advance.

Upvotes: 1

Views: 2216

Answers (4)

greenapps
greenapps

Reputation: 11214

 Intent.EXTRA_STREAM, image i want to send);

As soon as you use EXTRA_STREAM you are adding an attachment.

If you want to have your image in the body of the email you should send a html mail. Not a plain text mail.

Upvotes: 0

Patel Hiren
Patel Hiren

Reputation: 305

Try out as below:

Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.setType("image/*");
Uri uri = Uri.parse("android.resource://your package name/"+R.drawable.ic_launcher);
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
shareIntent.putExtra(android.content.Intent.EXTRA_EMAIL,recipients);
shareIntent.putExtra(Intent.EXTRA_TEXT, message);
startActivity(Intent.createChooser(shareIntent, "Send your image"));

EDITED:

Declare the File variable like below

         File pic;

In your OnActivityResult() apply changes as below:

Bundle ext = data.getExtras();
    bmpEmail = (Bitmap)ext.get("data");
    try {
        File root = Environment.getExternalStorageDirectory();
        if (root.canWrite()){
             pic = new File(root, "pic.png");
            FileOutputStream out = new FileOutputStream(pic);
            bmpEmail.compress(CompressFormat.PNG, 100, out);
            out.flush();
            out.close();
        }
    } catch (IOException e) {
        Log.e("BROKEN", "Could not write file " + e.getMessage());
    }   

And in your send email code add the line

      emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(pic));

Upvotes: 1

King of Masses
King of Masses

Reputation: 18765

try this

ImageView iv = (ImageView) findViewById(R.id.splashImageView);
Drawable d =iv.getBackground();
BitmapDrawable bitDw = ((BitmapDrawable) d);
Bitmap bitmap = bitDw.getBitmap();
File  mFile = savebitmap(bitmap);

and then

Uri u = null;
   u = Uri.fromFile(mFile);

   Intent emailIntent = new Intent(Intent.ACTION_SEND);
   emailIntent.setType("image/*");
   emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Hello...");
   // + "\n\r" + "\n\r" +
   // feed.get(Selectedposition).DETAIL_OBJECT.IMG_URL
   emailIntent.putExtra(Intent.EXTRA_TEXT, "Your tsxt here");
   emailIntent.putExtra(Intent.EXTRA_STREAM, u);
   startActivity(Intent.createChooser(emailIntent, "Send email..."));

and savebitmap method

   private File savebitmap(Bitmap bmp) {
  String extStorageDirectory = Environment.getExternalStorageDirectory().toString();
  OutputStream outStream = null;
  File file = new File(extStorageDirectory, temp + ".png");
  if (file.exists()) {
   file.delete();
   file = new File(extStorageDirectory, temp + ".png");
  }

  try {
   outStream = new FileOutputStream(file);
   bmp.compress(Bitmap.CompressFormat.PNG, 100, outStream);
   outStream.flush();
   outStream.close();
  } catch (Exception e) {
   e.printStackTrace();
   return null;
  }
  return file;
 }

Upvotes: 0

nomanr
nomanr

Reputation: 3775

You've to pass fileUri as the second argument. Like this.

intent.putExtra(Intent.EXTRA_STREAM, fileUri);

Upvotes: 1

Related Questions