我想要为Android实现某种远程协助工具(类似于vnc)。是否有可能在设备上通过编程来截取屏幕?
你可以尝试类似这样的代码:
View v = view.getRootView();
v.setDrawingCacheEnabled(true);
Bitmap b = v.getDrawingCache();
关于这个问题有一篇长篇讨论,在android-developers上,但简短的答案是:目前无法以编程方式拍摄安卓设备屏幕的截图,除非
Android清单权限READ_FRAME_BUFFER存在(请参见api文档此处),但目前只能由系统应用程序使用。其中各种原因之一是它存在安全风险。如果后台可以随时拍摄手机屏幕的截图,那么人们就可以使用OCR技术来嗅探用户输入的密码和其他私人信息。
因此,目前没有没有root权限的情况下VNC应用程序是不可能的。要从计算机中获取截图(当手机通过USB连接时),您可以使用DDMS。
Bitmap bitmap = takeScreenshot();
saveBitmap(bitmap);
编写函数...
public Bitmap takeScreenshot() {
View rootView = findViewById(android.R.id.content).getRootView();
rootView.setDrawingCacheEnabled(true);
return rootView.getDrawingCache();
}
public void saveBitmap(Bitmap bitmap) {
File imagePath = new File(Environment.getExternalStorageDirectory() + "/screenshot.png");
FileOutputStream fos;
try {
fos = new FileOutputStream(imagePath);
bitmap.compress(CompressFormat.PNG, 100, fos);
fos.flush();
fos.close();
} catch (FileNotFoundException e) {
Log.e("GREC", e.getMessage(), e);
} catch (IOException e) {
Log.e("GREC", e.getMessage(), e);
}
}
对于“一些”安卓设备(主要是三星),存在VNC类型的远程工具:
TeamViewer QuickSupport
https://play.google.com/store/apps/details?id=com.teamviewer.quicksupport.market
有人知道该工具如何进行屏幕捕捉,以及为什么仅支持有限的设备吗?