title: Capturing the current activity screen on android
body: <p>I am trying to capture the screen of the current activity so that I can use android's share sheet to share the image. My activity contains a map fragment and TextViews. Everything I have tried captures a screenshot with <a href="http://i.stack.imgur.com/mc8UM.png" rel="nofollow">a grid</a>
   (which appears black in the gallery). How should I go about capturing the entire screen ? Here is my XML file and java code used:</p>
  
  <pre><code>&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical"
  android:weightSum="15"
  android:gravity="bottom"
  android:id="@+id/rootview"
  android:background="#e7ebed"&gt;
  
  &lt;LinearLayout
      android:orientation="vertical"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:id="@+id/under_root"
      android:layout_weight="1"&gt;
  
      &lt;fragment
      xmlns:map="http://schemas.android.com/apk/res-auto"
      xmlns:tools="http://schemas.android.com/tools"
      android:id="@+id/map2"
      android:name="com.google.android.gms.maps.SupportMapFragment"
      android:layout_width="match_parent"
      android:layout_height="330dp"
      tools:context="com.github.pires.obd.reader.activity.TripReport" /&gt;
  
      &lt;LinearLayout
          android:orientation="vertical"
          android:layout_width="match_parent"
          android:layout_height="227dp"
          android:layout_weight="1"&gt;
  
          &lt;LinearLayout
              android:orientation="horizontal"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:layout_weight="0.66"&gt;
  
              &lt;TextView
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:textAppearance="?android:attr/textAppearanceMedium"
                  android:text="Average speed"
                  android:id="@+id/avgspeed"
                  android:layout_weight="1"
                  android:textColor="#000000" /&gt;
  
              &lt;TextView
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:textAppearance="?android:attr/textAppearanceMedium"
                  android:text="Engine Runtime"
                  android:id="@+id/runtime"
                  android:layout_weight="1"
                  android:textColor="#000000" /&gt;
          &lt;/LinearLayout&gt;
  
          &lt;LinearLayout
              android:orientation="horizontal"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:layout_weight="0.66"&gt;
  
              &lt;TextView
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:textAppearance="?android:attr/textAppearanceMedium"
                  android:text="Distance"
                  android:id="@+id/dist"
                  android:layout_weight="1"
                  android:textColor="#000000" /&gt;
  
              &lt;TextView
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:textAppearance="?android:attr/textAppearanceMedium"
                  android:text="Idle Time"
                  android:id="@+id/idletime"
                  android:layout_weight="1"
                  android:textColor="#000000" /&gt;
          &lt;/LinearLayout&gt;
  
          &lt;LinearLayout
              android:orientation="horizontal"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:layout_weight="0.66"&gt;
  
              &lt;TextView
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:textAppearance="?android:attr/textAppearanceMedium"
                  android:id="@+id/totaltime"
                  android:layout_weight="1" /&gt;
  
              &lt;ImageView
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:id="@+id/imageView2"
                  android:layout_weight="1" /&gt;
          &lt;/LinearLayout&gt;
  
      &lt;/LinearLayout&gt;
  &lt;/LinearLayout&gt;
  
  &lt;LinearLayout
      android:orientation="horizontal"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_weight="1"
      android:gravity="bottom"&gt;
  
      &lt;Button
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="Share"
          android:id="@+id/sharebutton"
          android:layout_weight="1"
          android:background="#7986cb"
          android:textColor="#ffffff"
          android:onClick="shareAction" /&gt;
  
      &lt;Button
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="Quit"
          android:id="@+id/butt_quit"
          android:layout_weight="1"
          android:textColor="#ffffff"
          android:background="#e53935"
          android:onClick="exitApp" /&gt;
  &lt;/LinearLayout&gt;
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>Here is the code:</p>
  
  <pre><code>       public void takeScreenShot()
  {
      View v1 = ll;
      Bitmap bm = captureScreen(v1);
      String pat="";
      try {
          pat = saveImage(bm);
      }
      catch(IOException e)
      {
  
      }
      Uri temp = Uri.parse(pat);
      Intent sharingIntent = new Intent(Intent.ACTION_SEND);
      sharingIntent.setType("image/jpeg");
      sharingIntent.putExtra(Intent.EXTRA_STREAM, temp);
      startActivity(Intent.createChooser(sharingIntent, "Share image using"));
  }
  public static Bitmap captureScreen(View v) {
  
      Bitmap screenshot = null;
      try {
  
          if(v!=null) {
  
                screenshot=Bitmap.createBitmap(v.getMeasuredWidth(),v.getMeasuredHeight(),    Bitmap.Config.ARGB_8888);
              Canvas canvas = new Canvas(screenshot);
              v.draw(canvas);
          }
  
      }catch (Exception e){
  
       }
  
      return screenshot;
  }
  
  public static String saveImage(Bitmap bitmap) throws IOException {
  
      ByteArrayOutputStream bytes = new ByteArrayOutputStream();
      bitmap.compress(Bitmap.CompressFormat.PNG, 40, bytes);
      File f = new File(Environment.getExternalStorageDirectory() + File.separator + "test.png");
      String path  = f.getAbsolutePath();
      f.createNewFile();
      FileOutputStream fo = new FileOutputStream(f);
      fo.write(bytes.toByteArray());
      fo.close();
      return path;
  }
  </code></pre>