如何直接获取Runtime.getRuntime().exec的结果 [英] How to get the result of Runtime.getRuntime().exec directly

查看:931
本文介绍了如何直接获取Runtime.getRuntime().exec的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究一个有 root 权限的 Android 设备.我正在尝试捕获屏幕并将结果存储在 Bitmap 中以备后用.

I'm working on a rooted android device. I'm trying to capture the screen and store the result in Bitmap for later usage.

String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getPath();
path += "/img.png";

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

final Bitmap x = BitmapFactory.decodeFile(path);

我在这里所做的是为新图像命名路径并使用命令 /system/bin/screencap -p FILEPATH 捕获屏幕.然后我读取我存储在该文件中的图像并在位图中使用它.

What I'm doing here is naming a path for a new image and capturing the screen using the command /system/bin/screencap -p FILEPATH. Then I read the image I stored in that file and use it in the bitmap.

我当前代码的问题是它很慢(不适合实时应用程序).我现在正在努力让它更快.我想直接从 Runtime.getRuntime().exec(...)

My problem with my current code is that it's slow(not suitable for a real-time application). I'm now trying to make it faster. Instead of saving the captured picture into file and then reading it again from the program, I want to read it directly from the result of Runtime.getRuntime().exec(...)

在命令的描述中screencap,我发现不用指定输出文件名就可以使用了,这种情况下结果会打印到stdout.

In the description of the command screencap, I found that I can use it without specifying the output file name, and in this case the results will be printed to stdout.

我尝试了几个代码来读取结果字节数组以直接在我的代码中使用

I tried several codes to read the result byte array to use it directly in my code

final Bitmap x = BitmapFactory.decodeByteArray(resultArrayByte, 0, resultArrayByte.length);

但没有一个代码对我有用.

but none of the codes worked with me.

如何使用 sh 的输入/输出流直接获取结果字节数组,而无需将输出保存到文件中然后再次加载?

How can I use sh's input/output streams to get the result byte array directly without saving the output into a file then loading it again?

推荐答案

看一看 此处,在此链接中您可以找到一个名为 ASL 的库

Take a Look here, in this link you can find a library called ASL

这篇文章中有很多问题,我很困惑:)

a lot of questions in this post, i'm confused :)

我希望此链接对您的要求有用.

i hope this link is useful for your requirements.

这篇关于如何直接获取Runtime.getRuntime().exec的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆