title: Android take screenshot on rooted device
body: <p><strong>UPDATE</strong> There are a number of other posts asking how to get a Screenshot in android but none seemed to have a full answer of how to do so. Originally I posted this as a question due to a particular issue I was running into while attempting to open a stream to the Frame Buffer. Now I've swapped over to dumping the Frame Buffer to a file so I've updated my post to show how I got there. For reference (and acknowledgement), I found the command to send the FrameBuffer to a file from <a href="https://stackoverflow.com/q/10813548/1446554">this post</a> (unfortunately he didn't provide how he got to that point). I'm just missing how to turn the raw data I pulled from the Frame Buffer into an actual image file.</p>
  
  <p>My intention was to take a full dump of the actual screen on an Android Device. The <strong>only</strong> way I could find to do so without using the adb bridge was to directly access the Frame Buffer of the system. Obviously this approach will require root privileges on the device and for the app running it! Fortunately for my purposes I have control over how the Device is set up and having the device rooted with root privileges provided to my application is feasible. My testing is currently being done on an old Droid running 2.2.3.</p>
  
  <p>I found my first hints of how to approach it from <a href="https://stackoverflow.com/a/6970338/1446554">https://stackoverflow.com/a/6970338/1446554</a>. After a bit more research I found another article that describes how to properly <a href="https://stackoverflow.com/a/5584154/1446554">run shell commands as root</a>. They were using it to execute a reboot, I use it to send the current frame buffer to an actual file. My current testing has only gotten as far as doing this via ADB and in a basic Activity (each being provided root). I will be doing further testing from a Service running in the background, updates to come! Here is my entire test activity that can export the current screen to a file:</p>
  
  <pre><code>public class ScreenshotterActivity extends Activity {
      public static final String TAG = "ScreenShotter";
  
      private Button _SSButton;
      private PullScreenAsyncTask _Puller;
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.main);
  
  
          _SSButton = (Button)findViewById(R.id.main_screenshotButton);
          _SSButton.setOnClickListener(new OnClickListener() {
  
              public void onClick(View v) {
                  if (_Puller != null)
                      return;
                  //TODO: Verify that external storage is available! Could always use internal instead...
  
                  _Puller = new PullScreenAsyncTask();
                  _Puller.execute((Void[])null);
              }
          });
      }
  
      private void runSuShellCommand(String cmd) {
          Runtime runtime = Runtime.getRuntime();
          Process proc = null;
          OutputStreamWriter osw = null;
          StringBuilder sbstdOut = new StringBuilder();
          StringBuilder sbstdErr = new StringBuilder();
  
          try { // Run Script
              proc = runtime.exec("su");
              osw = new OutputStreamWriter(proc.getOutputStream());
              osw.write(cmd);
              osw.flush();
              osw.close();
          } catch (IOException ex) {
              ex.printStackTrace();
          } finally {
              if (osw != null) {
                  try {
                      osw.close();
                  } catch (IOException e) {
                      e.printStackTrace();                    
                  }
              }
          }
  
          try {
              if (proc != null)
                  proc.waitFor();
          } catch (InterruptedException e) {
              e.printStackTrace();
          }
  
          sbstdOut.append(readBufferedReader(new InputStreamReader(proc.getInputStream())));
          sbstdErr.append(readBufferedReader(new InputStreamReader(proc.getErrorStream())));
      }
  
      private String readBufferedReader(InputStreamReader input) {
  
          BufferedReader reader = new BufferedReader(input);
          StringBuilder found = new StringBuilder();
          String currLine = null;
          String sep = System.getProperty("line.separator");
          try {
              // Read it all in, line by line.
              while ((currLine = reader.readLine()) != null) {
                  found.append(currLine);
                  found.append(sep);
              }
          } catch (IOException e) {
              e.printStackTrace();
          } finally {
              try {
                  reader.close();
              } catch (IOException e) {
                  e.printStackTrace();
              }
          }
  
          return null;
      }
  
      class PullScreenAsyncTask extends AsyncTask&lt;Void, Void, Void&gt; {
  
          @Override
          protected Void doInBackground(Void... params) {
  
              File ssDir = new File(Environment.getExternalStorageDirectory(), "/screenshots");
              if (ssDir.exists() == false) {
                  Log.i(TAG, "Screenshot directory doesn't already exist, creating...");
                  if (ssDir.mkdirs() == false) {
                      //TODO: We're kinda screwed... what can be done?
                      Log.w(TAG, "Failed to create directory structure necessary to work with screenshots!");
                      return null;
                  }
              }
              File ss = new File(ssDir, "ss.raw");            
              if (ss.exists() == true) {
                  ss.delete();
                  Log.i(TAG, "Deleted old Screenshot file.");
              }
              String cmd = "/system/bin/cat /dev/graphics/fb0 &gt; "+ ss.getAbsolutePath();
              runSuShellCommand(cmd);
              return null;
          }
  
          @Override
          protected void onPostExecute(Void result) {
              super.onPostExecute(result);
              _Puller = null;
          }
      }
  }
  </code></pre>
  
  <p>This also requires adding the <code>android.permission.WRITE_EXTERNAL_STORAGE</code> permission to the Manifest. As suggested in <a href="https://stackoverflow.com/a/4435708/1446554">this post</a>. Otherwise it runs, doesn't complain, doesn't create the directories nor the file.</p>
  
  <p>Originally I couldn't get usable data from the Frame Buffer due to not understanding how to properly run shell commands. Now that I've swapped to using the streams for executing commands I can use '>' to send the Frame Buffer's current data to an actual file... </p>