title: Send a bitmap image in an email
body: <p>I looked over similar questions and tried to write this simple code.
  This code takes an bitmap image of the view then allows the user to attach it to email. 
  for some reason I get the message "can't attach an empty file" in the email application. 
  I'm fairly new to this and I read a few examples and tried different things but non worked. I suspect that something might be wrong with my path.</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.example.asamater.myapplication" &gt;
  
  &lt;application
      android:allowBackup="true"
      android:icon="@mipmap/ic_launcher"
      android:label="@string/app_name"
      android:theme="@style/AppTheme" &gt;
      &lt;activity
          android:name=".MainActivity"
          android:label="@string/app_name" &gt;
          &lt;intent-filter&gt;
              &lt;action android:name="android.intent.action.MAIN" /&gt;
  
              &lt;category android:name="android.intent.category.LAUNCHER" /&gt;
          &lt;/intent-filter&gt;
      &lt;/activity&gt;
  &lt;/application&gt;
  </code></pre>
  
  <p></p>
  
  <pre><code>&lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"   android:layout_width="match_parent"
  android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
  android:paddingRight="@dimen/activity_horizontal_margin"
  android:paddingTop="@dimen/activity_vertical_margin"
  android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"&gt;
  
  &lt;ScrollView
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:id="@+id/scrollView"
      android:layout_centerVertical="true"
      android:layout_centerHorizontal="true" &gt;
  
      &lt;LinearLayout
          android:orientation="vertical"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:id="@+id/linearLayout"&gt;
  
          &lt;TextView
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:id="@+id/editText"
              android:text="Q1" /&gt;
  
          &lt;RadioGroup
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:layout_marginBottom="10dp"&gt;
  
              &lt;RadioButton
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:text="New RadioButton"
                  android:id="@+id/radioButton" /&gt;
  
              &lt;RadioButton
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:text="New RadioButton"
                  android:id="@+id/radioButton2" /&gt;
          &lt;/RadioGroup&gt;
  
          &lt;TextView
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:id="@+id/editText2"
              android:text="Q2" /&gt;
  
          &lt;RadioGroup
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:layout_marginBottom="10dp"&gt;
  
              &lt;RadioButton
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:text="New RadioButton"
                  android:id="@+id/radioButton3" /&gt;
  
              &lt;RadioButton
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:text="New RadioButton"
                  android:id="@+id/radioButton4" /&gt;
          &lt;/RadioGroup&gt;
  
          &lt;TextView
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:id="@+id/editText3"
              android:text="Q3" /&gt;
  
          &lt;RadioGroup
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:layout_marginBottom="10dp"&gt;
  
              &lt;RadioButton
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:text="New RadioButton"
                  android:id="@+id/radioButton5" /&gt;
  
              &lt;RadioButton
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:text="New RadioButton"
                  android:id="@+id/radioButton6" /&gt;
          &lt;/RadioGroup&gt;
  
          &lt;TextView
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:id="@+id/editText4"
              android:text="Q3" /&gt;
  
  
          &lt;RadioGroup
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:layout_marginBottom="10dp"&gt;
  
              &lt;RadioButton
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:text="New RadioButton"
                  android:id="@+id/radioButton7" /&gt;
  
              &lt;RadioButton
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:text="New RadioButton"
                  android:id="@+id/radioButton8" /&gt;
          &lt;/RadioGroup&gt;
  
          &lt;TextView
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:id="@+id/editText5"
              android:text="Q4" /&gt;
  
          &lt;RadioGroup
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:layout_marginBottom="10dp"&gt;
  
              &lt;RadioButton
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:text="New RadioButton"
                  android:id="@+id/radioButton10" /&gt;
  
              &lt;RadioButton
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:text="New RadioButton"
                  android:id="@+id/radioButton9" /&gt;
          &lt;/RadioGroup&gt;
  
          &lt;Button
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="Email"
              android:id="@+id/button"
              android:layout_alignTop="@id/radioButton9"
              android:onClick="email" /&gt;
  
      &lt;/LinearLayout&gt;
  
  
  
  &lt;/ScrollView&gt;
  
  &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p>.</p>
  
  <pre><code> package com.example.asamater.myapplication;
  
   import android.app.Activity;
   import android.content.Intent;
   import android.graphics.Bitmap;
   import android.graphics.Canvas;
   import android.graphics.Color;
   import android.graphics.drawable.Drawable;
  import android.net.Uri;
  import android.os.Environment;
  import android.provider.MediaStore;
  import android.support.v7.app.AppCompatActivity;
  import android.os.Bundle;
  import android.util.Log;
  import android.view.Menu;
  import android.view.MenuItem;
  import android.view.View;
  import android.widget.HorizontalScrollView;
  import android.widget.ScrollView;
  
  import java.io.File;
  import java.io.FileNotFoundException;
  import java.io.FileOutputStream;
  import java.util.BitSet;
  import java.util.Random;
  
  public class MainActivity extends AppCompatActivity {
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
  }
  
  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
      // Inflate the menu; this adds items to the action bar if it is present.
      getMenuInflater().inflate(R.menu.menu_main, menu);
      return true;
  }
  
  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
      // Handle action bar item clicks here. The action bar will
      // automatically handle clicks on the Home/Up button, so long
      // as you specify a parent activity in AndroidManifest.xml.
      int id = item.getItemId();
  
      //noinspection SimplifiableIfStatement
      if (id == R.id.action_settings) {
          return true;
      }
  
      return super.onOptionsItemSelected(item);
  }
  
  
  public void email(View view){
      ScrollView z = (ScrollView) findViewById(R.id.scrollView);
      int totalHeight = z.getChildAt(0).getHeight();
      int totalWidth = z.getChildAt(0).getWidth();
      Bitmap bitmap = getBitmapFromView(z,totalHeight,totalWidth);
      String path = saveImageToStorage(bitmap);
  
      Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
      emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, "ammar5001@gmail.com");
      emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "report");
      emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, " ");
  
      emailIntent.setType("image/jpeg");
      File bitmapFile = new File(Environment.getExternalStorageDirectory()+
              "/"+ path);
      Uri myUri = Uri.fromFile(bitmapFile);
      emailIntent.putExtra(Intent.EXTRA_STREAM, myUri);
  
  
      startActivity(Intent.createChooser(emailIntent, "Send your email in:"));
  
  }
  
  
  private String saveImageToStorage(Bitmap bitmap) {
      String root = Environment.getExternalStorageDirectory().toString();
      File myDir = new File(root + "/req_images");
      myDir.mkdirs();
      String fname = "Image-Report.jpg";
      File file = new File(myDir, fname);
      Log.i("myActivity", "" + file);
      if (file.exists())
          file.delete();
      try {
          FileOutputStream out = new FileOutputStream(file);
          bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
          out.flush();
          out.close();
          return root + "/req_images" + fname;
      } catch (Exception e) {
          e.printStackTrace();
          return null;
      }
  }
  
  
  public Bitmap getBitmapFromView(View view, int totalHeight, int totalWidth) {
      Bitmap returnedBitmap = Bitmap.createBitmap(totalWidth,totalHeight , Bitmap.Config.ARGB_8888);
      Canvas canvas = new Canvas(returnedBitmap);
      Drawable bgDrawable = view.getBackground();
      if (bgDrawable != null)
          bgDrawable.draw(canvas);
      else
          canvas.drawColor(Color.WHITE);
      view.draw(canvas);
      return returnedBitmap;
  }
  </code></pre>
  
  <p>}</p>