我在顶部有一个按钮,它允许我探索我的android ui,我正在尝试为它添加一个截图操作,并且向按钮添加动作的部分工作正常,什么不是work是截图代码,但是当我将此代码添加到"myactivity"
按钮时,此代码可以正常工作,但是当我正在探索android ui时,当这个动作在我的allways上的顶部按钮时,它给了我一个没有数据的黑色图像(0 kb)。这是我的截图代码:
View content = findViewById(android.R.id.content).getRootView();
content.setDrawingCacheEnabled(true);
Bitmap bitmap = content.getDrawingCache();
File file = new File( Environment.getExternalStorageDirectory() + "/test.png");
try
{
file.createNewFile();
FileOutputStream ostream = new FileOutputStream(file);
bitmap.compress(CompressFormat.PNG, 100, ostream);
ostream.close();
}
catch (Exception e)
{
e.printStackTrace();
}
问题可能是content
或。{
也许这不能在没有root访问权限的情况下完成,但如果是root访问权限
需要,你能给我一个示例代码吗?
答案 0 :(得分:0)
以下是在root设备上通过shell命令拍摄屏幕截图的代码 'fileDestination'是您希望保存屏幕截图文件的位置的路径。
try {
Process process = Runtime.getRuntime().exec("su", null,null);
OutputStream os = process.getOutputStream();
os.write(("/system/bin/screencap -p " + fileDestination).getBytes("ASCII"));
os.flush();
os.close();
process.waitFor();
} catch (IOException e) {
Log.d(TAG,LOG_LABEL+" IOException e:: SCREENSHOT FAILED");
e.printStackTrace();
} catch (InterruptedException e) {
Log.d(TAG,LOG_LABEL+"InterruptedException e:: SCREENSHOT FAILED");
e.printStackTrace();
}