title: Writing custom Android ViewAction to take screenshot
body: <p>I'm trying to take a screenshot before I perform an action in Android using espresso.</p>
  
  <pre><code>protected T performAction(ViewAction viewAction) {
      ViewAction screenShotAction = new ScreenShotAction();
      viewInteraction.perform(screenShotAction);
      viewInteraction.perform(viewAction);
      return returnGeneric();
  }
  </code></pre>
  
  <p>For example if in my test I perform a click() then I would take a screenshot of the device before I performed the click().</p>
  
  <p>This is the code for taking the screenshot in the ScreenShotAction class</p>
  
  <pre><code>@Override
  public void perform(UiController uiController, View view) {
  
      View rootView = view.getRootView();
      String state = Environment.getExternalStorageState();
      if(Environment.MEDIA_MOUNTED.equals(state)) {
          File picDir = new File(Environment.getExternalStorageDirectory() + "app_" + "test");
  
          if (!picDir.exists()) {
              picDir.mkdir();
          }
  
          rootView.setDrawingCacheEnabled(true);
          rootView.buildDrawingCache(true);
          Bitmap bitmap = rootView.getDrawingCache();
          String fileName = "test.jpg";
          File picFile = new File(picDir + "/" + fileName);
  
          try {
              picFile.createNewFile();
              FileOutputStream picOut = new FileOutputStream(picFile);
              bitmap = Bitmap.createBitmap(rootView.getWidth(), rootView.getHeight(), Bitmap.Config.ARGB_8888);
              boolean saved = bitmap.compress(Bitmap.CompressFormat.JPEG, 100, picOut);
  
              if (saved) {
                  // good
              } else {
                  // error
                  throw  new Exception("Image not saved");
              }
  
              picOut.flush();
              picOut.close();
  
          } catch (Exception e) {
              e.printStackTrace();
          }
          rootView.destroyDrawingCache();
      }
  }
  </code></pre>
  
  <p>I do not see any image files in the phone's Pictures directory or any other directory. I believe the screenshot method is solid but am unsure if I am calling the method correctly.</p>
  
  <p>Is <strong>viewInteraction.perform(screenShotAction)</strong> the corret way to call my custom view action?</p>
  
  <p>Please help and thank you in advance.</p>