我正在开发一个Android应用程序,以便在Android developer guide的帮助下录制视频。我程序上的所有代码都与此页面相同。

我在<application>标记之外为此定义了权限。

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


当应用程序启动时,相机预览正在运行。但是当我按下recorde按钮时,给出了NullPointerException。例外:

E/VIDEO_RECORDER(4782): Camera creating error :Fail to connect to camera service
D/AndroidRuntime(4782): Shutting down VM
W/dalvikvm(4782): threadid=1: thread exiting with uncaught exception (group=0x4001e578)
E/AndroidRuntime(4782): FATAL EXCEPTION: main
java.lang.NullPointerException
at com.timico.video.CameraActivity.prepareVideoRecorder(CameraActivity.java:115)
at com.timico.video.CameraActivity.access$5(CameraActivity.java:110)
at com.timico.video.CameraActivity$1.onClick(CameraActivity.java:69)
at android.view.View.performClick(View.java:2538)
at android.view.View$PerformClick.run(View.java:9152)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3691)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:907)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:665)
at dalvik.system.NativeStart.main(Native Method)


115行是mCamera.unlock();

您能否让我知道,这可能是什么原因(在Samsung galaxy s2和Nexus s上测试)?谢谢

public static Camera getCameraInstance(){
        Camera c = null;
        try{
            c = Camera.open();
        } catch(Exception e){
            Log.e(TAG, "Camera creating error :" + e.getMessage());
        }
        return c;
    }

    private boolean prepareVideoRecorder(){

        mCamera = getCameraInstance();
        mCamera.unlock();

        mMediaRecorder = new MediaRecorder();
        mMediaRecorder.setCamera(mCamera);
        mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
        mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
        mMediaRecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH));
        mMediaRecorder.setOutputFile(getOutputMediaFile(MEDIA_TYPE_VIDEO).toString());
        mMediaRecorder.setPreviewDisplay(mPreview.getHolder().getSurface());

        try {
            mMediaRecorder.prepare();
        } catch (IllegalStateException e) {
            Log.d(TAG, "IllegalStateException preparing MediaRecorder: " + e.getMessage());
            releaseMediaRecorder();
            return false;
        } catch (IOException e) {
            Log.d(TAG, "IOException preparing MediaRecorder: " + e.getMessage());
            releaseMediaRecorder();
            return false;
        }
        return true;
    }


这就是创建我的表面的方式。

public void surfaceCreated(SurfaceHolder holder) {
       try {
            mCamera.setPreviewDisplay(holder);
            mCamera.startPreview();
       } catch (IOException e) {
                Log.d(TAG, "Error setting camera preview: " + e.getMessage());
       }
}

最佳答案

我知道已经晚了,但是对您有帮助。

mCamera = Camera.open(0);之前使用mcamera.unlock();并在设备而不是模拟器上对其进行测试。

08-06 11:09