onepiece
onepiece

Reputation: 3539

Android: Camera with SurfaceView

I am trying to use Android's Camera with a SurfaceView, so the the user can preview the camera. I followed various tutorials on StackOverflow, and finally ended up with Google's own tutorial, but my app crashed. Here is my code. I am using Eclipse's Android Virtual Device, emulated camera (which I don't think would affect anything) and 100mb sd card space alloted.

There are two classes, a CameraPreview object and the CameraActivity.

public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback{

    private SurfaceHolder mHolder;
    private Camera mCamera;
    private final String TAG = "TAG";

    public CameraPreview(Context context, Camera camera) {
        super(context);
        mCamera = camera;

        // Install a SurfaceHolder.Callback so we get notified when the
        // underlying surface is created and destroyed.
        mHolder = getHolder();
        mHolder.addCallback(this);
        // deprecated setting, but required on Android versions prior to 3.0
        mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    }

    public void surfaceCreated(SurfaceHolder holder) {
        // The Surface has been created, now tell the camera where to draw the preview.
        try {
            mCamera.setPreviewDisplay(holder);
            mCamera.startPreview();
        } catch (IOException e) {
            Log.d(TAG, "Error setting camera preview: " + e.getMessage());
        }
    }

    public void surfaceDestroyed(SurfaceHolder holder) {
        // empty. Take care of releasing the Camera preview in your activity.
    }

    public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
        // If your preview can change or rotate, take care of those events here.
        // Make sure to stop the preview before resizing or reformatting it.

        if (mHolder.getSurface() == null){
          // preview surface does not exist
          return;
        }

        // stop preview before making changes
        try {
            mCamera.stopPreview();
        } catch (Exception e){
          // ignore: tried to stop a non-existent preview
        }

        // set preview size and make any resize, rotate or
        // reformatting changes here

        // start preview with new settings
        try {
            mCamera.setPreviewDisplay(mHolder);
            mCamera.startPreview();

        } catch (Exception e){
            Log.d(TAG, "Error starting camera preview: " + e.getMessage());
        }
    }
}


public class CameraActivity extends Activity{
 private Camera mCamera;
    private CameraPreview mPreview;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Create an instance of Camera
        mCamera = getCameraInstance();

        // Create our Preview view and set it as the content of our activity.
        mPreview = new CameraPreview(this, mCamera);
        FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
        preview.addView(mPreview);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    public Camera getCameraInstance(){
        Camera c = null;
        try {
            c = Camera.open(); // attempt to get a Camera instance
        }
        catch (Exception e){
            // Camera is not available (in use or does not exist)
        }
        return c; // returns null if camera is unavailable
    }
}

06-04 14:28:07.726: W/dalvikvm(6115): threadid=1: thread exiting with uncaught exception (group=0x40a71930) 06-04 14:28:07.956: E/AndroidRuntime(6115): FATAL EXCEPTION: main 06-04 14:28:07.956: E/AndroidRuntime(6115): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.mobilequote/com.example.mobilequote.CameraActivity}: java.lang.NullPointerException 06-04 14:28:07.956: E/AndroidRuntime(6115): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180) 06-04 14:28:07.956: E/AndroidRuntime(6115): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) 06-04 14:28:07.956: E/AndroidRuntime(6115): at android.app.ActivityThread.access$600(ActivityThread.java:141) 06-04 14:28:07.956: E/AndroidRuntime(6115): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234) 06-04 14:28:07.956: E/AndroidRuntime(6115): at android.os.Handler.dispatchMessage(Handler.java:99) 06-04 14:28:07.956: E/AndroidRuntime(6115): at android.os.Looper.loop(Looper.java:137) 06-04 14:28:07.956: E/AndroidRuntime(6115): at android.app.ActivityThread.main(ActivityThread.java:5041) 06-04 14:28:07.956: E/AndroidRuntime(6115): at java.lang.reflect.Method.invokeNative(Native Method) 06-04 14:28:07.956: E/AndroidRuntime(6115): at java.lang.reflect.Method.invoke(Method.java:511) 06-04 14:28:07.956: E/AndroidRuntime(6115): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 06-04 14:28:07.956: E/AndroidRuntime(6115): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 06-04 14:28:07.956: E/AndroidRuntime(6115): at dalvik.system.NativeStart.main(Native Method) 06-04 14:28:07.956: E/AndroidRuntime(6115): Caused by: java.lang.NullPointerException 06-04 14:28:07.956: E/AndroidRuntime(6115): at com.example.mobilequote.CameraActivity.onCreate(CameraActivity.java:24) 06-04 14:28:07.956: E/AndroidRuntime(6115): at android.app.Activity.performCreate(Activity.java:5104) 06-04 14:28:07.956: E/AndroidRuntime(6115): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080) 06-04 14:28:07.956: E/AndroidRuntime(6115): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144) 06-04 14:28:07.956: E/AndroidRuntime(6115): ... 11 more

The line CameraActivity.java:24 is preview.addView(mPreview);

Upvotes: 2

Views: 3487

Answers (1)

spartygw
spartygw

Reputation: 3462

See my comment about the emulator. But the reason your app crashes is because you catch the exception of the camera failing to open and return null. Then in your CameraPreview class you save this null value to 'mCamera' and then later dereference it in surfaceCreated:

mCamera.setPreviewDisplay(holder);

So your original question is why is it crashing...it's crashing because you are not handling error conditions well.

Upvotes: 1

Related Questions