简体   繁体   English

Android从代码中截取屏幕截图,具有root访问权限

[英]Android take screenshot from code, with root access

I need to take a screenshot from a service running in the background. 我需要从后台运行的服务中截取屏幕截图。 The screenshot can be of the app in the foreground, even if its not my app, which is why I can't use DrawingCache. 屏幕截图可以是前景中的应用程序,即使它不是我的应用程序,这就是我无法使用DrawingCache的原因。 I've searched around and found this 我四处寻找并找到了这个

process = Runtime.getRuntime().exec("su -c cat /dev/graphics/fb0"); InputStream is = process.getInputStream();

But I don't know what to do with the stream. 但我不知道如何处理流。 How do I get it into a bitmap? 如何将其添加到位图中? I don't need to save it to file I just need to keep it in a variable. 我不需要将它保存到文件中我只需要将其保存在变量中。 This is for rooted phones. 这适用于root手机。

Any help is much appreciated. 任何帮助深表感谢。

Well, seems I'm on my own with this one. 嗯,似乎我自己就是这个。 So far this is what I've found, for those who have a similar problem. 到目前为止,这是我发现的,对于那些有类似问题的人。 Note that this varies by version, and I am running 2.3 Gingerbread. 请注意,这因版本而异,我正在运行2.3 Gingerbread。

I found 2 files in /system/bin - screencap and screenshot . 我在/system/bin找到了2个文件 - screencapscreenshot

screencap returns a bitmap in a format I don't know how to decode, while screenshot makes a regular bmp file called tmpshot.bmp at the root of the sdcard (Neither accept any kind of command parameters. They don't seem to accept -h,-help, --help etc either). screencap以一种我不知道如何解码的格式返回一个位图,而screenshot在sdcard的根目录下生成一个名为tmpshot.bmp的常规bmp文件(既不接受任何类型的命令参数。它们似乎不接受 - h,-help, - help等)。

tmpshot.bmp seems a viable solution for now but I was looking to avoid writing and reading from sdcard as this is quite the bottleneck. tmpshot.bmp现在似乎是一个可行的解决方案,但我希望避免写入和读取SD卡,因为这是相当的瓶颈。 I will keep searching, but for those who need a solution now, here you are. 我会一直在寻找,但对于那些现在需要解决方案的人来说,你就是这样。

EDIT: Upon further trials I found the screencap binary from another rom (MIUI to be specific) actually offers a few more options, where (miui) screencap -t png /sdcard/screen.png yields a valid png screenshot. 编辑:经过进一步的试验,我发现另一个rom的screencap二进制文件(MIUI是特定的)实际上提供了一些选项,其中(miui) screencap -t png /sdcard/screen.png产生一个有效的png截图。

Try this 尝试这个

 Process sh = Runtime.getRuntime().exec("su", null,null);    
                    OutputStream  os = sh.getOutputStream();
                    os.write(("/system/bin/screencap -p " + "/sdcard/img.png").getBytes("ASCII"));
                    os.flush();          
                    os.close();
                    sh.waitFor();

put it in a try catch block 把它放在try catch块中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM