Get screenshot of surfaceView in Android

ぃ、小莉子 提交于 2019-11-27 07:02:55

问题


In my application I have view with some buttons and SurfaceView. I must take screenshot of this view, but in place of SurfaceView is only black field. I try solutions which I found on stackoverflow, but they don't work for me.

This is my code which I usualy take 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);
        }
    }
}

Maybe someone can help.

来源:https://stackoverflow.com/questions/15160598/get-screenshot-of-surfaceview-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!