Programatically running : getRuntime.exec("screencap", "-p /sdcard/image.png")
does not do anything except exiting with code=1
.
But when USB debugging, running that same command from pc in cmd: adb shell screencap -p /sdcard/dddd.png
works.
From previous stackoverflow posts, It seems that programatcially running that command doesn't work because the app does not have permission, and this requires rooting the phone to give su access to the app.
Why does running these commands from the pc doesn't require privileged access ?
Difference is that adbd
(adb daemon) runs with shell
uid, while doing that from the app you're still with your app's uid. So it's different contexts.
There is an SELinux rule that allows adb to invoke the screencap( defined in system/sepolicy/private/adbd.te ):
# Perform binder IPC to surfaceflinger (screencap)
# XXX Run screencap in a separate domain?
binder_use(adbd)
binder_call(adbd, surfaceflinger)
binder_call(adbd, gpuservice)
# b/13188914
allow adbd gpu_device:chr_file rw_file_perms;
allow adbd ion_device:chr_file rw_file_perms;
r_dir_file(adbd, system_file)