Obter captura de tela do surfaceView no Android

Na minha aplicação eu tenho visão com alguns botões e SurfaceView. Eu preciso tirar screenshots dessa visão, mas no lugar do SurfaceView é apenas um campo preto. Eu tento soluções que encontrei no stackoverflow, mas elas não funcionam para mim.

Este é o meu código que eu costumo tirar screenshots:

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);
        }
    }
}

Talvez alguém possa ajudar.

questionAnswers(0)

yourAnswerToTheQuestion