Take Screenshot of SurfaceView

前端 未结 6 2094
既然无缘
既然无缘 2020-11-27 17:32

Am developing a simple camera app. I have code that takes screenshot of the whole activity and writes it to the Sd card. the problem is that the Surfaceview returns a black

6条回答
  •  情话喂你
    2020-11-27 18:03

    Just copy and past code

    Note: only For API level >= 24

    private void takePhoto() {
    
            // Create a bitmap the size of the scene view.
            final Bitmap bitmap = Bitmap.createBitmap(surfaceView.getWidth(), surfaceView.getHeight(),
                    Bitmap.Config.ARGB_8888);
    
    
    
            // Create a handler thread to offload the processing of the image.
            final HandlerThread handlerThread = new HandlerThread("PixelCopier");
            handlerThread.start();
            // Make the request to copy.
            PixelCopy.request(holder.videoView, bitmap, (copyResult) -> {
                if (copyResult == PixelCopy.SUCCESS) {
                    Log.e(TAG,bitmap.toString());
                    String name = String.valueOf(System.currentTimeMillis() + ".jpg");
                    imageFile = ScreenshotUtils.store(bitmap,name);
    
                } else {
                    Toast toast = Toast.makeText(getViewActivity(),
                            "Failed to copyPixels: " + copyResult, Toast.LENGTH_LONG);
                    toast.show();
                }
                handlerThread.quitSafely();
            }, new Handler(handlerThread.getLooper()));
        }
    

提交回复
热议问题