Have SurfaceView :

 this.surf = (SurfaceView) findViewById(R.id.surf); this.surf.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); this.surf.addCallback(new Callback() { @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { Cam.this.h = holder; } @Override public void surfaceCreated(SurfaceHolder holder) { Cam.this.h = holder; } @Override public void surfaceDestroyed(SurfaceHolder holder) { } }); 

and there is a MediaRecorder recording video:

 MediaRecorder mMediaRecorder = new MediaRecorder(); mMediaRecorder.setCamera(Cam.this.mCamera); mMediaRecorder.setAudioSource(AudioSource.MIC); mMediaRecorder.setVideoSource(VideoSource.CAMERA); mMediaRecorder.setPreviewDisplay(Cam.this.surf); mMediaRecorder.setOutputFormat(OutputFormat.MPEG_4); mMediaRecorder.setAudioEncoder(AudioEncoder.DEFAULT); mMediaRecorder.setVideoEncoder(VideoEncoder.H264); mMediaRecorder.setVideoSize(1280, 720); mMediaRecorder.setOutputFile(Cam.this.mParcelFileDescriptor[1].getFileDescriptor()); mMediaRecorder.setMaxDuration(60000); mMediaRecorder.prepare(); mMediaRecorder.start(); 

There are no problems with video recording. But, for some reason, when you receive a screenshot of the application, you can see that instead of SurfaceView a black rectangle is displayed. Here is the screen capture code:

 View v = findViewById(R.id.mainLayout); // основная компоновка (в моём случае RelativeLayout) v.setDrawingCacheEnabled(true); v.buildDrawingCache(true); Bitmap b = Bitmap.createBitmap(v.getDrawingCache()); v.setDrawingCacheEnabled(false); FileOutputStream o = new FileOutputStream(new File(Environment.getExternalStorageDirectory(), "frame.jpeg")); b.compress(CompressFormat.JPEG, 100, o); o.flush(); o.close(); 

and the XML for this Activity:

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/mainLayout" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="test.Cam" > <SurfaceView android:id="@+id/surface" android:layout_width="match_parent" android:layout_height="match_parent" /> </RelativeLayout> 

How to fix the error, make sure that SurfaceView not displayed as a black rectangle in the screenshot?


I know one good option that requires root rights:

su / system / bin / screencap -p /sdcard/screenshot.jpf

Immediately the option does not fit, since root is not right.

  • second thought - why do you need a screenshot at all? maybe you really need snapshots in the recording process, for some event from the user? for this, there is a regular way. - tse
  • @tse, which way? Yes, I need a snapshot of the camera during video recording. If possible, tell me how. On the Internet they say it is impossible. MediaRecorder blocks the camera object for the entire usage time and mCamera.takeScreenshot(..) throw an exception. - nick
  • Um, it seems I was wrong, in the sense of my memories are already outdated. I did this, but now nothing like the way I did, not googled. But I found this answer: stackoverflow.com/a/36321147/1263771 - tse
  • @tse, there is the Camera2 API, and this is Android 5 and higher. We need a minimum of no higher than 4, that is, no higher than API 16. - nick

0