Get screenshot of current foreground app on Android having root priviliges

前端 未结 2 1926
梦谈多话
梦谈多话 2021-01-26 13:55

I\'m developing an application that is installed on the system partition and I would like to know if it\'s possible to get a screenshot of the current foregroun

2条回答
  •  失恋的感觉
    2021-01-26 14:37

    Months have passed since I asked this question but just now had the time to add this feature. The way to do this is simply by calling screencap -p and then grabbing the file. Next is the code I used:

    private class WorkerTask extends AsyncTask {
        @Override
        protected File doInBackground(String... params) {
            File screenshotFile = new File(Environment.getExternalStorageDirectory().getPath(), SCREENSHOT_FILE_NAME);
            try {
                Process screencap = Runtime.getRuntime().exec("screencap -p " + screenshotFile.getAbsolutePath());
                screencap.waitFor();
                return screenshotFile;
            } catch (IOException ioe) {
                ioe.printStackTrace();
            } catch (InterruptedException ie) {
                ie.printStackTrace();
            }
    
            return null;
        }
    
        @Override
        protected void onPostExecute(File screenshot_file) {
            // Do something with the file.
        }
    }
    

    Remember to add the permission to the manifest. Otherwise screenshot.png will be blank.

    This is much simpler than what Goran stated and is what I finally used.

    Note: It only worked for me when the app is installed on the system partition.

提交回复
热议问题