Camera.startPreview crashes and reboots phone afte

2019-04-10 17:51发布

问题:

I have an Activity that opens the Camera and starts a preview on a SurfaceTexture. Everything works fine, but I've noticed that if I repeatedly leave the activity and return to it, after a handful of times, the phone freezes and then reboots.

I've narrowed the problem down to the call to startPreview. And I get two ominous log messages right before the issue happens:

01-19 10:20:52.038: E/IMGSRV(22777): :0: __map: Map device memory failed
01-19 10:20:52.038: W/GraphicBufferMapper(22777): registerBuffer(0x70b750) failed -14 (Bad address)

Has anyone seen this before? Is this an issue with the Galaxy Nexus hardware or Android 4.0? If so, are there any work arounds?

Note: My testing is on Android 4.0, with a Galaxy Nexus.

Edit - Solved:

Turns out it was a memory leak due to OpenGL. All the examples on the web I could find use the following code to clean up after OpenGL.

try { mEgl.eglDestroyContext(mEglDisplay, mEglContext); } catch (Throwable t) {}
try { mEgl.eglDestroySurface(mEglDisplay, mEglSurface); } catch (Throwable t) {}

This leaks the surface, and as a result after some number of tries, which varies per phone, will cause OpenGL to fail to initialize. For instance it would fail after 32 tries on the Nexus S, but only 8 tries on the LG Optimus.

After some trial and error I found that the following code fixed the issue:

mEgl.eglMakeCurrent(mEglDisplay, EGL10.EGL_NO_SURFACE, EGL10.EGL_NO_SURFACE, EGL10.EGL_NO_CONTEXT);

Note: On the Galaxy Nexus instead of getting a nice OpenGL error which I could display to the user it just seemed to crash on startPreview. I assume this was memory related, but the above fixed cleared it up as well.

回答1:

The SDK docs for the android.hardware.Camera class say the following:

Important: Call release() to release the camera for use by other applications. Applications should release the camera immediately in onPause() (and re-open() it in onResume()).

Are you certain your activity releases the camera in onPause() and re-opens it in onResume()?

If you post a code sample, we would be better able to troubleshoot your problem.