title: Taking screenshot programmatically in android
body: <p>I am taking screenshot programmatically using the following code:</p>
  
  <pre><code>public static Bitmap takeScreenshot(View view)
      {
          try
          {
              // create bitmap screen capture
              view.setDrawingCacheEnabled(true);
              Bitmap bitmap = Bitmap.createBitmap(view.getDrawingCache());
              view.setDrawingCacheEnabled(false);
              return bitmap;
          }
          catch (Throwable e)
          {
              CustomLogHandler.printError(e);
          }
          return null;
      }
  
  private static void copyFile(Bitmap bitmap)
      {
          File dstFile = getShareResultFile();
  
          //Delete old file if exist.
          if(dstFile.exists()) {
              dstFile.delete();
          }
  
          FileOutputStream fos = null;
          try
          {
              fos = new FileOutputStream(dstFile);
              bitmap.compress(Bitmap.CompressFormat.JPEG, 0, fos);
              fos.flush();
          }
          catch (Exception e) {
              CustomLogHandler.printError(e);
          }
          finally {
              if (fos != null) {
                  try {
                      fos.close();
                  } catch (IOException ioe) {
                      CustomLogHandler.printError(ioe);
                  }
              }
          }
      }
  </code></pre>
  
  <p>There are several problem like:</p>
  
  <ol>
  <li>Back arrow, title and share menu background color is not correct. It looks messy.</li>
  <li>Background color of toolbar is totally changed.</li>
  <li>Image quality is too poor and list items rounded drawable has not smooth corners.</li>
  <li>Background of layout is not taken that I set as background of my parent layout.</li>
  </ol>
  
  <p>I am taking the screenshot from the root view.</p>
  
  <p><a href="https://i.stack.imgur.com/pfuE0.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/pfuE0.png" alt="ma"></a></p>