尝试从字段中读取' int android.media.CamcorderProfile.fileFormat'在null对象引用上

时间:2015-07-31 06:45:29

标签: android video camera video-recording

我在Nexus 7 2012上使用自定义相机录制视频并获得此问题,而其他设备,如Zenfone 2,Galaxy S3,S6,HTC One M7,Oppo N1,Sony Xperia M4 ...工作完美。

public static boolean prepareVideoRecorder(Context mContext, int mode) {
    // Should release before use new Preview for Recording Video mode
    CustomCamera.releaseCamera();

    // Initialize camera
    CustomCamera.mCamera = CustomCamera.getCameraInstance(mode);

    CustomCamera.mCamera.startPreview();

    // Set orientation display
    setCameraDisplayOrientation((Activity) mContext, mode);

    // Should release before use new Preview for Recording Video mode
    CustomCamera.releaseMediaRecorder();

    CustomCamera.mMediaRecorder = new MediaRecorder();

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

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

    // Step 3: Set a CamcorderProfile (requires API Level 8 or higher)
    // todo NullPointerException: Attempt to read from field 'int android.media.CamcorderProfile.fileFormat' on a null object reference
    //     

   // returned true value, go inside
   if (CamcorderProfile.hasProfile(current_camera_id, CamcorderProfile.QUALITY_LOW)) {
    // CamcorderProfile.get(CamcorderProfile.QUALITY_LOW) RETURNED NULL VALUE, SO I CAN NOT SET PROFILE     
    CustomCamera.mMediaRecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_LOW));

    // Step 4: Set output file - pass False value to indicate choose Video mode
    RECORDED_FILE_PATH = getOutputMediaFile(mContext, false).toString();
    CustomCamera.mMediaRecorder.setOutputFile(RECORDED_FILE_PATH);

    // Step 5: Set the preview output
    /**
     * Define Orientation of image in here,
     * if in portrait mode, use value = 90,
     * if in landscape mode, use value = 0
     */
    if (current_camera_id == define.Camera.CAMERA_BACK)
        // Back Camera
        CustomCamera.mMediaRecorder = Utils.rotateBackVideo(CustomCamera.mMediaRecorder);
    else
        // Front Camera
        CustomCamera.mMediaRecorder = Utils.rotateFrontVideo(CustomCamera.mMediaRecorder);

    // Set preview display to refresh current screen
    CustomCamera.mMediaRecorder.setPreviewDisplay(mCameraPreview.getHolder().getSurface());

    // Step 6: Prepare configured MediaRecorder
    try {
        CustomCamera.mMediaRecorder.prepare();
    } catch (IllegalStateException e) {
        e.printStackTrace();
        CustomCamera.releaseMediaRecorder();
        return false;
    } catch (IOException e) {
        e.printStackTrace();
        CustomCamera.releaseMediaRecorder();
        return false;
    }

    return true;
}

我设置的这些权限:

<uses-feature android:name="android.hardware.camera.front" />
<uses-feature android:name="android.hardware.microphone"/>

<uses-permission android:name = "android.permission.CAMERA" />
<uses-permission android:name = "android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name = "android.permission.RECORD_AUDIO" />
<uses-permission android:name = "android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name = "android.permission.WRITE_INTERNAL_STORAGE" />

我想要的是让它可以在Nexus 7 2012上录制视频。

知道的人,

请告诉我如何解决。

谢谢,

p / s:如果我删除setProfile()行,我会收到另一个例外而无法录制视频:

07-31 14:33:39.936  25960-25960/com.panasonic_Upload E/MediaRecorder﹕ setOutputFile called in an invalid state(2)
07-31 14:33:39.942  25960-25960/com.panasonic_Upload W/System.err﹕ java.lang.IllegalStateException
07-31 14:33:39.950  25960-25960/com.panasonic_Upload W/System.err﹕ at android.media.MediaRecorder._setOutputFile(Native Method)
07-31 14:33:39.950  25960-25960/com.panasonic_Upload W/System.err﹕ at android.media.MediaRecorder.prepare(MediaRecorder.java:740)
07-31 14:33:39.950  25960-25960/com.panasonic_Upload W/System.err﹕ at ui.fragment.camera.CameraPreviewFragment.prepareVideoRecorder(CameraPreviewFragment.java:533)
07-31 14:33:39.950  25960-25960/com.panasonic_Upload W/System.err﹕ at ui.fragment.camera.CameraPreviewFragment.onClick(CameraPreviewFragment.java:223)
07-31 14:33:39.950  25960-25960/com.panasonic_Upload W/System.err﹕ at android.view.View.performClick(View.java:4780)
07-31 14:33:39.951  25960-25960/com.panasonic_Upload W/System.err﹕ at android.view.View$PerformClick.run(View.java:19866)
07-31 14:33:39.951  25960-25960/com.panasonic_Upload W/System.err﹕ at android.os.Handler.handleCallback(Handler.java:739)
07-31 14:33:39.951  25960-25960/com.panasonic_Upload W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:95)
07-31 14:33:39.951  25960-25960/com.panasonic_Upload W/System.err﹕ at android.os.Looper.loop(Looper.java:135)
07-31 14:33:39.951  25960-25960/com.panasonic_Upload W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java:5254)
07-31 14:33:39.951  25960-25960/com.panasonic_Upload W/System.err﹕ at java.lang.reflect.Method.invoke(Native Method)
07-31 14:33:39.951  25960-25960/com.panasonic_Upload W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:372)
07-31 14:33:39.952  25960-25960/com.panasonic_Upload W/System.err﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
 07-31 14:33:39.952  25960-25960/com.panasonic_Upload W/System.err﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

1 个答案:

答案 0 :(得分:2)

调用get()时必须传递摄像头ID,与调用hasProfile()时完全相同。否则它将从它找到的第一个后置摄像头获取配置文件。

http://developer.android.com/reference/android/media/CamcorderProfile.html#pubmethods

Nexus 7 2012崩溃,因为它没有后置摄像头。

你需要这样做:

CustomCamera.mMediaRecorder.setProfile(CamcorderProfile.get(current_camera_id, CamcorderProfile.QUALITY_LOW));