Camera doesn't work after starting second acti

2019-08-14 16:05发布

问题:

I started working with the new camera2 API.

I run a camera preview on a TextureView in my first Activity. Via my navigation drawer can I start a second Activity. I want this one to show me a camera preview in a TextureView, too. Unfortunately, the screen stays white and I don't see a picture. I don't know the error and couldn't find something about it:

5278-5414/de.application E/CameraDeviceGLThread-0﹕ Received exception on GL render thread:
    java.lang.IllegalStateException: swapBuffers: EGL error: 0x300d
            at android.hardware.camera2.legacy.SurfaceTextureRenderer.checkEglError(SurfaceTextureRenderer.java:487)
            at android.hardware.camera2.legacy.SurfaceTextureRenderer.swapBuffers(SurfaceTextureRenderer.java:480)
            at android.hardware.camera2.legacy.SurfaceTextureRenderer.drawIntoSurfaces(SurfaceTextureRenderer.java:681)
            at android.hardware.camera2.legacy.GLThreadManager$1.handleMessage(GLThreadManager.java:103)
            at android.os.Handler.dispatchMessage(Handler.java:98)
            at android.os.Looper.loop(Looper.java:211)
            at android.os.HandlerThread.run(HandlerThread.java:61) 06-01 23:00:44.258    5278-5414/de.application I/CameraDeviceState﹕ Legacy camera service transitioning to state ERROR

I start the new Activity pretty simple, and the functions for the camera are in both Activitys more or less the same.

Intent i = new Intent(MainActivityOld.this, FullScreenActivity.class);
startActivity(i);

Can you imagine a mistake I make? Do I have to stop the first Camera Preview somehow?

回答1:

Generally, a TextureView tears down its SurfaceTexture output when it is no longer displayed. So when your first Activity goes into the background, the TextureView is no longer a valid target for camera data.

How were you intending to send preview data to your second Activity's TextureView? You'll need to switch the flow of preview buffers to target the second TextureView when the switchover occurs. You can do this by creating a new camera capture session that targets the second TextureView, when you switch to the second activity.