在我的应用程序中,我有一些按钮和SurfaceView查看.我必须截取这个视图的截图,但代替SurfaceView只是黑场.我尝试在stackoverflow上找到的解决方案,但它们对我不起作用.
这是我的代码,我通常会截取屏幕截图:
File folder = new File(Environment.getExternalStorageDirectory().getPath() + "/folder");
if (!folder.exists()) {
folder.mkdir();
}
String path = folder.getAbsolutePath() + "snapshot.png";
File file = new File(path);
file.createNewFile();
View view = this.findViewById(android.R.id.content).getRootView();
view.setDrawingCacheEnabled(true);
Bitmap drawingCache = view.getDrawingCache();
Bitmap bitmap = Bitmap.createBitmap(drawingCache);
FileOutputStream fos = null;
try {
fos = new FileOutputStream(file);
bitmap.compress(CompressFormat.PNG, 100, fos);
} finally {
bitmap.recycle();
view.setDrawingCacheEnabled(false);
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
Log.e(TAG, e.getMessage(), e);
}
}
}
也许有人可以帮忙.