用于图片和视频的相机不工作

时间:2013-09-09 09:15:45

标签: android video android-camera

我在Android应用程序中使用相机拍摄照片和视频。我正在使用以下代码,但它强制关闭应用程序。 LogCat和代码如下所示。如果有人可以提供帮助,我们将非常感激。

logcat的:

        09-09 14:32:08.246: E/AndroidRuntime(4764): FATAL EXCEPTION: main
        09-09 14:32:08.246: E/AndroidRuntime(4764): java.lang.NullPointerException
        09-09 14:32:08.246: E/AndroidRuntime(4764):     at com.example.cameravideo.VideoCaptureActivity.initRecorder(VideoCaptureActivity.java:72)
        09-09 14:32:08.246: E/AndroidRuntime(4764):     at com.example.cameravideo.VideoCaptureActivity.surfaceCreated(VideoCaptureActivity.java:100)
        09-09 14:32:08.246: E/AndroidRuntime(4764):     at android.view.SurfaceView.updateWindow(SurfaceView.java:571)
        09-09 14:32:08.246: E/AndroidRuntime(4764):     at android.view.SurfaceView.access$000(SurfaceView.java:86)
        09-09 14:32:08.246: E/AndroidRuntime(4764):     at android.view.SurfaceView$3.onPreDraw(SurfaceView.java:175)
        09-09 14:32:08.246: E/AndroidRuntime(4764):     at android.view.ViewTreeObserver.dispatchOnPreDraw(ViewTreeObserver.java:833)
        09-09 14:32:08.246: E/AndroidRuntime(4764):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1860)
        09-09 14:32:08.246: E/AndroidRuntime(4764):     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1004)
        09-09 14:32:08.246: E/AndroidRuntime(4764):     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5481)
        09-09 14:32:08.246: E/AndroidRuntime(4764):     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
        09-09 14:32:08.246: E/AndroidRuntime(4764):     at android.view.Choreographer.doCallbacks(Choreographer.java:562)
        09-09 14:32:08.246: E/AndroidRuntime(4764):     at android.view.Choreographer.doFrame(Choreographer.java:532)
        09-09 14:32:08.246: E/AndroidRuntime(4764):     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
        09-09 14:32:08.246: E/AndroidRuntime(4764):     at android.os.Handler.handleCallback(Handler.java:730)
        09-09 14:32:08.246: E/AndroidRuntime(4764):     at android.os.Handler.dispatchMessage(Handler.java:92)

代码:

        public class VideoCaptureActivity extends Activity implements
                SurfaceHolder.Callback {

            private final String VIDEO_PATH_NAME = "/mnt/sdcard/VGA_30fps_512vbrate.mp4";
            private MediaRecorder mMediaRecorder;
            private Camera mCamera;
            private SurfaceView mSurfaceView;
            private SurfaceHolder mHolder;
            private View mToggleButton;
            private boolean mInitSuccesful;

            @Override
            public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);         setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
     mSurfaceView = (SurfaceView) findViewById(R.id.surface_camera);

mHolder = mSurfaceView.getHolder();
     mHolder.addCallback(this);
     mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
     mToggleButton = (ToggleButton) findViewById(R.id.toggleButton);
     mToggleButton.setOnClickListener(new OnClickListener()
     {
        public void onClick(View v) {
                if (((ToggleButton) v).isChecked()) {
                    mMediaRecorder.start();
                    try {
                        Thread.sleep(10 * 1000);
                    }
        catch (Exception e) {
                e.printStackTrace();
                    }
                    finish();
                        }
                        else {
                    mMediaRecorder.stop();
                    mMediaRecorder.reset();
            try {

initRecorder(mHolder.getSurface());
                            }
        catch (IOException e) {
            e.printStackTrace();
                    }
                }
        }
                });
            }

            private void initRecorder(Surface surface) throws IOException {
                if (mCamera == null) {
                    mCamera = Camera.open();
                    mCamera.unlock();
                }
                if (mMediaRecorder == null)
                mMediaRecorder = new MediaRecorder();
                mMediaRecorder.setPreviewDisplay(surface);
                mMediaRecorder.setCamera(mCamera);
        mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);
    mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);          mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
                mMediaRecorder.setVideoEncodingBitRate(512 * 1000);
                mMediaRecorder.setVideoFrameRate(30);
                mMediaRecorder.setVideoSize(640, 480);
                mMediaRecorder.setOutputFile(VIDEO_PATH_NAME);

                try {
                    mMediaRecorder.prepare();
                }
                catch (IllegalStateException e) {
                    e.printStackTrace();
                }
                mInitSuccesful = true;
            }

            @Override
            public void surfaceCreated(SurfaceHolder holder) {
                try {
                    if (!mInitSuccesful)
                    initRecorder(mHolder.getSurface());
                }
                catch (IOException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void surfaceDestroyed(SurfaceHolder holder) {
                shutdown();
            }

            @Override
            public void surfaceChanged(SurfaceHolder holder, int format, int width,int height) {
            }

            private void shutdown() {
                mMediaRecorder.reset();
                mMediaRecorder.release();
                mCamera.release();
                mMediaRecorder = null;
                mCamera = null;
            }
        }

2 个答案:

答案 0 :(得分:0)

异常的原因可能是因为Camera.open()返回null!

       if (mCamera == null) {
            mCamera = Camera.open();
            mCamera.unlock();
        }

您的清单文件中是否包含以下内容?

<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />

如果您的api级别&gt; = 2.3

,请尝试使用Camera.open(int cameraId);

答案 1 :(得分:0)

尝试下面的代码,了解手机上有哪些CameraId可用,然后使用Id中的Camera.open(id)之一

    private void knowIfCameraIsAvailable() {

    int camNumber = Camera.getNumberOfCameras();
    System.out.println("Number of Camera = " + camNumber);

    for (int i = 0; i < camNumber; i++) {
        try {
            Log.e("Camera ID", "ID  ==  " + i);
            Camera cam = Camera.open(i);
            System.out.println("Camera object = " + cam);
            Camera.Parameters camParameter = cam.getParameters();
            System.out.println("camParameter.getMaxZoom() = "
                    + camParameter.getMaxZoom());
            cam.unlock();
            cam.release();
        } catch (NullPointerException e) {
            Log.i("NullPointer", "Object NULL");
            e.printStackTrace();
        } catch (IllegalStateException e) {
            Log.i("IllegalState", "Illegal State");
            e.printStackTrace();
        } catch (Exception e) {
            Log.i("Exception", "Other Exception");
            e.printStackTrace();
        }
    }
}