5

在我的应用程序中,我有一些按钮和 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);
        }
    }
}

也许有人可以帮忙。

4

0 回答 0