Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/217.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android:Screencap直接保存到字节-跳过保存到文件_Java_Android_Root_Screen Capture_Su - Fatal编程技术网

Java Android:Screencap直接保存到字节-跳过保存到文件

Java Android:Screencap直接保存到字节-跳过保存到文件,java,android,root,screen-capture,su,Java,Android,Root,Screen Capture,Su,我使用以下代码在根设备上拍摄屏幕截图: sh = Runtime.getRuntime().exec("su", null,null); os = sh.getOutputStream(); os.write(("/system/bin/screencap -p " + "/sdcard/img.jpg" + "\n").getBytes("ASCII")); os.flush(); os.close(); sh.waitFor(); m

我使用以下代码在根设备上拍摄屏幕截图:

    sh =  Runtime.getRuntime().exec("su", null,null);
    os = sh.getOutputStream();
    os.write(("/system/bin/screencap -p " + "/sdcard/img.jpg" + "\n").getBytes("ASCII"));
    os.flush();
    os.close();
    sh.waitFor();
    myBitmap = BitmapFactory.decodeFile("/sdcard/img.jpg");
由于我想经常拍摄屏幕截图,我不想先将其保存到文件中,然后再读取字节。有没有更好的方法直接获取屏幕截图位图?我只是不想消除这种不必要的耽搁。 非常感谢

更新:

可以查看screencap.cpp代码

此外,它还提到:

    "usage: %s [-hp] [-d display-id] [FILENAME]\n"
    " -h: this message\n"
    " -p: save the file as a png.\n"
    " -d: specify the display id to capture, default %d.\n"
    "If FILENAME ends with .png it will be saved as a png.\n"
    "If FILENAME is not given, the results will be printed to stdout.\n"

请帮帮我。谢谢

除非您使用的是8.1 Pixel2 XL,否则这将起作用,然后您将获得一个安全异常

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import java.io.IOException;
import java.io.OutputStreamWriter;

public class BitmapScreencap {
    public final static BitmapScreencap Get = new BitmapScreencap();
    private BitmapScreencap() { }
    public Bitmap Screen() {
        try {
            Process process = Runtime.getRuntime().exec("su");
            OutputStreamWriter outputStream = new OutputStreamWriter(process.getOutputStream());
            outputStream.write("/system/bin/screencap -p\n");
            outputStream.flush();
            Bitmap screen = BitmapFactory.decodeStream(process.getInputStream());
            outputStream.write("exit\n");
            outputStream.flush();
            outputStream.close();
            return screen;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}
在代码中的任意位置放置位图

BitmapScreencap.Get.Screen();

你可能可以用一个命名的管道做些什么。@ChrisStratton谢谢你,伙计,你能详细说明一下吗?虽然有点晚了,但你可以在这里查看我有关这个的问题和答案: