相机在android异常非法状态异常

时间:2013-07-22 09:05:02

标签: android

我正在制作一个录像机,并且logcat上的下一个异常引起了我的注意,我怀疑错误是在onClick函数中,因为代码在添加此函数之前正常工作。

示例代码在此处:

package com.example.video;

import java.io.IOException;
import android.media.CamcorderProfile;
import android.media.MediaRecorder;
import android.os.Build;
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Activity;
import android.hardware.Camera;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.FrameLayout;

 public class MainActivity extends Activity 

 {
 private Camera mCamera;
 private CameraPreview mPreview;
  MediaRecorder mMediaRecorder;
  private boolean isRecording = false;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
 // Create an instance of Camera
    mCamera = getCameraInstance();
    mPreview = new CameraPreview(this, mCamera);
    FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
    preview.addView(mPreview);

}

@TargetApi(Build.VERSION_CODES.GINGERBREAD)
public static Camera getCameraInstance(){
    int cameraCount = 0;
    Camera cam = null;
    Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
    cameraCount = Camera.getNumberOfCameras();
    for ( int camIdx = 0; camIdx < cameraCount; camIdx++ ) {
        Camera.getCameraInfo( camIdx, cameraInfo );
        if ( cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT  ) {
            try {
                cam = Camera.open( camIdx );
            } catch (RuntimeException e) {
                Log.e("error", "Camera failed to open: " + e.getLocalizedMessage());
            }
        }
    }

    return cam;
}

@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 void onClick(View v) 
{
    if (isRecording) {
        // stop recording and release camera
        mMediaRecorder.stop();  // stop the recording
        releaseMediaRecorder(); // release the MediaRecorder object
        mCamera.lock();         // take camera access back from MediaRecorder

        // inform the user that recording has stopped

        isRecording = false;
    } else {
        // initialize video camera
        if (prepareVideoRecorder()) {
            // Camera is available and unlocked, MediaRecorder is prepared,
            // now you can start recording
            mMediaRecorder.start();

            // inform the user that recording has started

            isRecording = true;
        } else {
            // prepare didn't work, release the camera
            releaseMediaRecorder();
            // inform user
        }
    }
}






@SuppressLint("InlinedApi")
private boolean prepareVideoRecorder(){

    mCamera = getCameraInstance();
    mMediaRecorder  = new MediaRecorder();

    // Step 1: Unlock and set camera to MediaRecorder
    mCamera.unlock();
    mMediaRecorder.setCamera(mCamera);

    // Step 2: Set sources
    mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
    mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);

    // Step 3: Set a CamcorderProfile (requires API Level 8 or higher)
    mMediaRecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH));

    // Step 4: Set output file
    mMediaRecorder.setOutputFile("D://");

    // Step 5: Set the preview output
    mMediaRecorder.setPreviewDisplay(mPreview.getHolder().getSurface());

    // Step 6: Prepare configured MediaRecorder
    try {
        mMediaRecorder.prepare();
    } catch (IllegalStateException e) {
        Log.d("error", "IllegalStateException preparing MediaRecorder: " + e.getMessage());
         releaseMediaRecorder();
        return false;
    } catch (IOException e) {
        Log.d("error", "IOException preparing MediaRecorder: " + e.getMessage());
        releaseMediaRecorder();
        return false;
    }
    mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
    mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
    mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.DEFAULT);
    return true;
}

private void releaseMediaRecorder(){
    if (mMediaRecorder != null) {
        mMediaRecorder.reset();   // clear recorder configuration
        mMediaRecorder.release(); // release the recorder object
        mMediaRecorder = null;
        mCamera.lock();           // lock camera for later use
    }
}

@Override
protected void onPause() {
    super.onPause();
    releaseMediaRecorder();       // if you are using MediaRecorder, release it first
    releaseCamera();              // release the camera immediately on pause event
}


private void releaseCamera(){
    if (mCamera != null){
        mCamera.release();        // release the camera for other applications
        mCamera = null;
    }
}



}

这是logcat窗口:

 07-22 10:56:08.243: 
 D/libEGL(31644): loaded /system/lib/egl/libEGL_VIVANTE.so
 07-22 10:56:08.250: D/libEGL(31644): loaded /system/lib/egl/libGLESv1_CM_VIVANTE.so
 07-22 10:56:08.260: D/libEGL(31644): loaded /system/lib/egl/libGLESv2_VIVANTE.so
 07-22 10:56:08.350: D/OpenGLRenderer(31644): Enabling debug mode 0
 07-22 10:56:13.083: E/error(31644): Camera failed to open: Fail to connect to camera    service
 07-22 10:56:13.093: D/AndroidRuntime(31644): Shutting down VM
  07-22 10:56:13.093: W/dalvikvm(31644): threadid=1: thread exiting with uncaught exception (group=0x40aa61f8)
 07-22 10:56:13.117: E/AndroidRuntime(31644): FATAL EXCEPTION: main
 07-22 10:56:13.117: E/AndroidRuntime(31644): java.lang.IllegalStateException: Could   not execute method of the activity
 07-22 10:56:13.117: E/AndroidRuntime(31644):   at   android.view.View$1.onClick(View.java:3052)
 07-22 10:56:13.117: E/AndroidRuntime(31644):at     android.view.View.performClick(View.java:3519)
 07-22 10:56:13.117: E/AndroidRuntime(31644):   at   android.view.View$PerformClick.run(View.java:14143)
 07-22 10:56:13.117: E/AndroidRuntime(31644):   at android.os.Handler.handleCallback(Handler.java:605)
 07-22 10:56:13.117: E/AndroidRuntime(31644):   at android.os.Handler.dispatchMessage(Handler.java:92)
 07-22 10:56:13.117: E/AndroidRuntime(31644):   at android.os.Looper.loop(Looper.java:137)
 07-22 10:56:13.117: E/AndroidRuntime(31644):   at android.app.ActivityThread.main(ActivityThread.java:4521)
 07-22 10:56:13.117: E/AndroidRuntime(31644):   at java.lang.reflect.Method.invokeNative(Native Method)
 07-22 10:56:13.117: E/AndroidRuntime(31644):   at java.lang.reflect.Method.invoke(Method.java:511)
 07-22 10:56:13.117: E/AndroidRuntime(31644):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:762)
 07-22 10:56:13.117: E/AndroidRuntime(31644):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:529)
 07-22 10:56:13.117: E/AndroidRuntime(31644):   at dalvik.system.NativeStart.main(Native Method)
 07-22 10:56:13.117: E/AndroidRuntime(31644): Caused by: java.lang.reflect.InvocationTargetException
 07-22 10:56:13.117: E/AndroidRuntime(31644):   at java.lang.reflect.Method.invokeNative(Native Method)
 07-22 10:56:13.117: E/AndroidRuntime(31644):   at java.lang.reflect.Method.invoke(Method.java:511)
 07-22 10:56:13.117: E/AndroidRuntime(31644):   at android.view.View$1.onClick(View.java:3047)
 07-22 10:56:13.117: E/AndroidRuntime(31644):   ... 11 more
 07-22 10:56:13.117: E/AndroidRuntime(31644): Caused by: java.lang.NullPointerException
 07-22 10:56:13.117: E/AndroidRuntime(31644):   at com.example.video.MainActivity.prepareVideoRecorder(MainActivity.java:109)
 07-22 10:56:13.117: E/AndroidRuntime(31644):   at com.example.video.MainActivity.onClick(MainActivity.java:81)
 07-22 10:56:13.117: E/AndroidRuntime(31644):   ... 14 more

0 个答案:

没有答案