Android在root设备上截屏

cgo*_*den 8 android screenshot root

更新有很多其他帖子询问如何在Android中获取屏幕截图但似乎没有一个完整的答案如何这样做.最初我把这个问题发布为一个问题,因为我在试图打开一个流到帧缓冲区时遇到了一个特定的问题.现在我已经交换了将Frame Buffer转储到一个文件中,所以我更新了我的帖子以显示我是如何到达那里的.为了参考(和确认),我找到了从这篇文章发送FrameBuffer到一个文件的命令(不幸的是他没有提供他如何达到这一点).我只是想知道如何将从Frame Buffer中提取的原始数据转换为实际的图像文件.

我的目的是在Android设备上完全转储实际屏幕.我没有使用adb桥就能找到这样做的唯一方法就是直接访问系统的Frame Buffer.显然,这种方法需要设备上的root权限以及运行它的应用程序!幸运的是,出于我的目的,我可以控制设备的设置方式,并使设备具有root权限,并且提供给我的应用程序是可行的.我的测试目前正在运行2.2.3的旧Droid上进行.

我从/sf/answers/487923691/找到了我的第一个如何处理它的提示.经过一番研究后,我发现另一篇文章描述了如何以root身份正确运行shell命令.他们用它来执行重启,我用它将当前帧缓冲区发送到实际文件.我目前的测试只是通过ADB和基本Activity(每个都提供root)来实现.我将从后台运行的服务进行进一步测试,更新来!这是我的整个测试活动,可以将当前屏幕导出到文件:

public class ScreenshotterActivity extends Activity {
    public static final String TAG = "ScreenShotter";

    private Button _SSButton;
    private PullScreenAsyncTask _Puller;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);


        _SSButton = (Button)findViewById(R.id.main_screenshotButton);
        _SSButton.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                if (_Puller != null)
                    return;
                //TODO: Verify that external storage is available! Could always use internal instead...

                _Puller = new PullScreenAsyncTask();
                _Puller.execute((Void[])null);
            }
        });
    }

    private void runSuShellCommand(String cmd) {
        Runtime runtime = Runtime.getRuntime();
        Process proc = null;
        OutputStreamWriter osw = null;
        StringBuilder sbstdOut = new StringBuilder();
        StringBuilder sbstdErr = new StringBuilder();

        try { // Run Script
            proc = runtime.exec("su");
            osw = new OutputStreamWriter(proc.getOutputStream());
            osw.write(cmd);
            osw.flush();
            osw.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        } finally {
            if (osw != null) {
                try {
                    osw.close();
                } catch (IOException e) {
                    e.printStackTrace();                    
                }
            }
        }

        try {
            if (proc != null)
                proc.waitFor();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        sbstdOut.append(readBufferedReader(new InputStreamReader(proc.getInputStream())));
        sbstdErr.append(readBufferedReader(new InputStreamReader(proc.getErrorStream())));
    }

    private String readBufferedReader(InputStreamReader input) {

        BufferedReader reader = new BufferedReader(input);
        StringBuilder found = new StringBuilder();
        String currLine = null;
        String sep = System.getProperty("line.separator");
        try {
            // Read it all in, line by line.
            while ((currLine = reader.readLine()) != null) {
                found.append(currLine);
                found.append(sep);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                reader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        return null;
    }

    class PullScreenAsyncTask extends AsyncTask<Void, Void, Void> {

        @Override
        protected Void doInBackground(Void... params) {

            File ssDir = new File(Environment.getExternalStorageDirectory(), "/screenshots");
            if (ssDir.exists() == false) {
                Log.i(TAG, "Screenshot directory doesn't already exist, creating...");
                if (ssDir.mkdirs() == false) {
                    //TODO: We're kinda screwed... what can be done?
                    Log.w(TAG, "Failed to create directory structure necessary to work with screenshots!");
                    return null;
                }
            }
            File ss = new File(ssDir, "ss.raw");            
            if (ss.exists() == true) {
                ss.delete();
                Log.i(TAG, "Deleted old Screenshot file.");
            }
            String cmd = "/system/bin/cat /dev/graphics/fb0 > "+ ss.getAbsolutePath();
            runSuShellCommand(cmd);
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);
            _Puller = null;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

这还需要向android.permission.WRITE_EXTERNAL_STORAGEManifest 添加权限.正如本文所述.否则它运行,不抱怨,不创建目录或文件.

最初由于不了解如何正确运行shell命令,我无法从帧缓冲区获取可用数据.现在我已经交换使用流来执行命令,我可以使用'>'将Frame Buffer的当前数据发送到实际文件......

Vis*_*nan 11

以编程方式,您可以运行"adb shell/system/bin/screencap -p /sdcard/img.png",如下所示:

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();
Run Code Online (Sandbox Code Playgroud)


Ben*_*ton 5

ICS设备的一种简单解决方案是从命令行使用以下内容

adb shell /system/bin/screencap -p /sdcard/screenshot.png
adb pull /sdcard/screenshot.png screenshot.png
Run Code Online (Sandbox Code Playgroud)

它将把screenshot.png文件保存在当前目录中。

在运行4.0.3的Samsung Galaxy SII上测试。


小智 3

对于不同的手机来说,这会有所不同。这取决于您设备的底层图形格式。您可以使用系统调用来轮询图形格式。如果您只想在知道其图形格式的设备上运行它,则可以编写一个转换器将其转换为已知格式。

您可以查看以下项目:http://code.google.com/p/android-fb2png/

如果您查看 fb2png.c 的源代码,您可以看到它们轮询 FBIOGET_VSCREENINFO,其中包含有关设备如何在内存中存储屏幕图像的信息。一旦您知道了这一点,您应该能够将其转换为您可以使用的格式。

我希望这有帮助。