简体   繁体   English

在Android上截取屏幕截图

[英]Take a screenshot on Android

I'm trying to build a custom launcher for android that needs to take a screenshot of the device right after the user presses the home button and consequently right before my app (the launcher) goes back to the foreground. 我正在尝试为Android构建一个自定义启动器,需要在用户按下主页按钮之后立即截取设备,因此在我的应用程序(启动器)返回前台之前。 Pretty much the idea is to take a screenshot of whatever the device was showing right before my launcher/activity/app took over the screen. 我的想法是在我的启动器/活动/应用程序接管屏幕之前截取设备显示的任何内容。

I am aware that such a thing requires root access, but that's not a problem. 我知道这样的事情需要root访问权限,但这不是问题。 Can anyone show me how to take screenshots? 谁能告诉我如何拍摄截图? Some sample code? 一些示例代码? A tutorial online? 在线教程? I have looked online but couldn't find anything useful. 我看过网上但找不到任何有用的东西。 I'm trying to simply take screenshots just like a gazillion apps on the Android Market already do with root access. 我试图简单地截取屏幕截图,就像Android Market上的gazillion应用程序已经使用root访问权限一样。 Thanks in advance :-) 提前致谢 :-)

This is a security breach. 这是一个安全漏洞。 You should have a rooted device in order to do it. 你应该有一个root设备才能做到这一点。

You can use this code with your own app: (I got this code from this answer to another question: https://stackoverflow.com/a/5651242/237838 ) 您可以将此代码与您自己的应用程序一起使用:(我从这个答案获得了另一个问题的代码: https//stackoverflow.com/a/5651242/237838

// image naming and path  to include sd card  appending name you choose for file
String mPath = Environment.getExternalStorageDirectory().toString() + "/" + ACCUWX.IMAGE_APPEND;   

// create bitmap screen capture
Bitmap bitmap;
View v1 = mCurrentUrlMask.getRootView();
v1.setDrawingCacheEnabled(true);
bitmap = Bitmap.createBitmap(v1.getDrawingCache());
v1.setDrawingCacheEnabled(false);

OutputStream fout = null;
imageFile = new File(mPath);

try {
   fout = new FileOutputStream(imageFile);
   bitmap.compress(Bitmap.CompressFormat.JPEG, 90, fout);
   fout.flush();
   fout.close();
} catch (FileNotFoundException e) {
   e.printStackTrace();
} catch (IOException e) {
   e.printStackTrace();
}

Try looking this code for additional help: 尝试查看此代码以获取其他帮助:

PS It's possible to take a screenshot from Android using DDMS, but this is another story. PS可以使用DDMS从Android获取屏幕截图,但这是另一个故事。

PPS If you want to draw your launcher transparently over the activity below it you need to set pixel format to RGBA (only dealt with OpenGL) and render accordingly. PPS如果要在下面的活动上透明地绘制启动器,则需要将像素格式设置为RGBA(仅处理OpenGL)并相应地进行渲染。 You don't need to take a screenshot of the underlaying activity to do that. 您无需截取底层活动的屏幕截图即可。

Rooted device: use this code on the rooted device: 根植设备:使用扎根设备上运行此代码:

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

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

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