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.
MediaRecorderblocks the camera object for the entire usage time andmCamera.takeScreenshot(..)throw an exception. - nickCamera2API, and this is Android 5 and higher. We need a minimum of no higher than 4, that is, no higher than API 16. - nick