Android's Camera Preview


This is a note to self post. Today I’ve been figuring out to have my own custom Camera Activity. A newbie mistake made the Activity to block the UI when initializing. Here’s the problem and the fix.

I started camera.startPreview() at surfaceCreated(SurfaceHolder holder). When I moved to surfaceChanged(SurfaceHolder holder, int format, int width, int height), everything turns out as expected. Here’s the full class with an explanation below.

To made matters worse, I called camera.startPreview() both when on both methods. The explanation on SurfaceHolder.Callback’s documentation was crystal clear.

surfaceCreated(SurfaceHolder holder)

This is called immediately after the surface is first created.

surfaceChanged(SurfaceHolder holder, int format, int width, int height)

This is called immediately after any structural changes (format or size) have been made to the surface.

The emphasis is the key.