Unable to take screenshot in Android L programmatically

I am using Nexus 5. Version 5.1.1

I want to take a screen shot programmatically from a phone at any given time. I am fine if the solution applies to Nexus 5.

After reading a lot of answers, I tried the solution below with screencap.

The problem is creating the image file, but it is empty.

private void takeSS() {
    try {

        if (isExternalStorageWritable()) {

            Process su = Runtime.getRuntime().exec("/system/bin/screencap -p /storage/emulated/0/img.png\n");
            DataOutputStream outputStream = new DataOutputStream(su.getOutputStream());

            outputStream.flush();
            su.waitFor();

            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(su.getInputStream()));

            String line;
            while ((line = bufferedReader.readLine()) != null) {
                String output = line + System.getProperty("line.separator");
                System.out.println(line);
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

/* Checks if external storage is available for read and write */
public boolean isExternalStorageWritable() {
    String state = Environment.getExternalStorageState();
    if (Environment.MEDIA_MOUNTED.equals(state)) {
        return true;
    }
    return false;
}

The same command, if I run from the command line, runs successfully. Please suggest what might be the problem.

PS - My phone is rooted.

thank

+4
source share
1 answer

Have you tried to execute a command like su?

"/system/bin/su -c \"/system/bin/screencap -p /storage/emulated/0/img.png\""

0
source

Source: https://habr.com/ru/post/1599353/


All Articles