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();
}
}
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
source
share