The problem is that Runtime.getRuntime().exec(...) does not know how to handle the shell language. On a Linux / Unix platform, you will have something like this:
Process p = Runtime.getRuntime().exec(new String[]{ "/bin/sh", "-c", "/system/bin/lsof | grep mediaserver"});
However (apparently) Android does not have a shell / command line by default. Therefore, either you need to identify and install a suitable shell on your device, or build the pipeline "manually"; that is, by creating a pipe file descriptor and executing two commands so that lsof writes to the pipe and grep lsof from it.
Maybe the answer is to run it like this ...
Process p = Runtime.getRuntime().exec( "adb shell /system/bin/lsof | grep mediaserver");
(Try running the "shell ..." part of adb interactively before doing this with Java.)
source share