2012-07-03 69 views
0

我試圖從我的平板電腦的前置攝像頭和其運行的2.2 Android Froyo中捕獲視頻。我真的無法弄清楚這個錯誤背後的原因。由於該平板電腦是Tegra 2 Nvigia芯片組可以導致問題,是平板電腦有前置攝像頭。請任何人給我一個方向休息,我會弄清楚。在Android 2.2的前端攝像頭視頻錄製失敗

這是我使用的代碼,

    Camera cam = openFrontFacingCamera(); 
        m_recorder = new MediaRecorder(); 
        cam.unlock(); 
        m_recorder.setCamera(cam); 
        m_recorder.setAudioSource(MediaRecorder.AudioSource.MIC); 
        m_recorder.setVideoSource(MediaRecorder.VideoSource.CAMERA); 
        m_recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); 
        m_recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); 
        m_recorder.setVideoEncoder(MediaRecorder.VideoEncoder.MPEG_4_SP); 
        m_recorder.setMaxDuration((int) 1000); 
        m_recorder.setVideoSize(320, 240); 
        m_recorder.setVideoFrameRate(15); 
        m_recorder.setOutputFile("/sdcard/myvideo"); 

        try { 
         m_recorder.prepare(); 
        } catch (IllegalStateException e) { 
         // TODO Auto-generated catch block 
         e.printStackTrace(); 
        } catch (IOException e) { 
         // TODO Auto-generated catch block 
         e.printStackTrace(); 
        } 

        // CRASHING HERE: 
        m_recorder.start(); 

這是我這個方法

   private Camera openFrontFacingCamera() { 
        Camera camera = null; 

        // Look for front-facing camera, using the Gingerbread API. 
        // Java reflection is used for backwards compatibility with pre-Gingerbread APIs. 
        try { 
         Class<?> cameraClass = Class.forName("android.hardware.Camera"); 
         Object cameraInfo = null; 
         Field field = null; 
         int cameraCount = 0; 
         Method getNumberOfCamerasMethod = cameraClass.getMethod("getNumberOfCameras"); 
         if (getNumberOfCamerasMethod != null) { 
          cameraCount = (Integer) getNumberOfCamerasMethod.invoke(null, (Object[]) null); 
         } 
         Class<?> cameraInfoClass = Class.forName("android.hardware.Camera$CameraInfo"); 
         if (cameraInfoClass != null) { 
          cameraInfo = cameraInfoClass.newInstance(); 
         } 
         if (cameraInfo != null) { 
          field = cameraInfo.getClass().getField("facing"); 
         } 
         Method getCameraInfoMethod = cameraClass.getMethod("getCameraInfo", Integer.TYPE, cameraInfoClass); 
         if (getCameraInfoMethod != null && cameraInfoClass != null && field != null) { 
          for (int camIdx = 0; camIdx < cameraCount; camIdx++) { 
           getCameraInfoMethod.invoke(null, camIdx, cameraInfo); 
           int facing = field.getInt(cameraInfo); 
           if (facing == 1) { // Camera.CameraInfo.CAMERA_FACING_FRONT 
            try { 
             Method cameraOpenMethod = cameraClass.getMethod("open", Integer.TYPE); 
             if (cameraOpenMethod != null) { 
              camera = (Camera) cameraOpenMethod.invoke(null, camIdx); 
             } 
            } catch (RuntimeException e) { 
             Log.e(TAG, "Camera failed to open: " + e.getLocalizedMessage()); 
            } 
           } 
          } 
         } 
        } 
        // Ignore the bevy of checked exceptions the Java Reflection API throws - if it fails, who cares. 
        catch (ClassNotFoundException e  ) {Log.e(TAG, "ClassNotFoundException" + e.getLocalizedMessage());} 
        catch (NoSuchMethodException e  ) {Log.e(TAG, "NoSuchMethodException" + e.getLocalizedMessage());} 
        catch (NoSuchFieldException e   ) {Log.e(TAG, "NoSuchFieldException " + e.getLocalizedMessage());} 
        catch (IllegalAccessException e  ) {Log.e(TAG, "IllegalAccessException" + e.getLocalizedMessage());} 
        catch (InvocationTargetException e ) {Log.e(TAG, "InvocationTargetException" + e.getLocalizedMessage());} 
        catch (InstantiationException e  ) {Log.e(TAG, "InstantiationException" + e.getLocalizedMessage());} 
        catch (SecurityException e   ) {Log.e(TAG, "SecurityException" + e.getLocalizedMessage());} 

        if (camera == null) { 
         // Try using the pre-Gingerbread APIs to open the camera. 
         try { 
          camera = Camera.open(); 
         } catch (RuntimeException e) { 
          Log.e(TAG, "Camera failed to open: " + e.getLocalizedMessage()); 
         } 
        } 

        return camera; 
       } 

之前使用相機初始化函數這是錯誤:

  07-03 08:01:09.220: A/dalvikvm(32523): Exception!!! threadid=1: thread exiting with uncaught exception (group=0x4001d860) 
      07-03 08:01:09.230: E/AndroidRuntime(32523): FATAL EXCEPTION: main 
      07-03 08:01:09.230: E/AndroidRuntime(32523): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.webvideo.com/com.webvideo.com.MainActivity}: java.lang.IllegalStateException 
      07-03 08:01:09.230: E/AndroidRuntime(32523): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663) 
      07-03 08:01:09.230: E/AndroidRuntime(32523): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679) 
      07-03 08:01:09.230: E/AndroidRuntime(32523): at android.app.ActivityThread.access$2300(ActivityThread.java:125) 
      07-03 08:01:09.230: E/AndroidRuntime(32523): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033) 
      07-03 08:01:09.230: E/AndroidRuntime(32523): at android.os.Handler.dispatchMessage(Handler.java:99) 
      07-03 08:01:09.230: E/AndroidRuntime(32523): at android.os.Looper.loop(Looper.java:123) 
      07-03 08:01:09.230: E/AndroidRuntime(32523): at android.app.ActivityThread.main(ActivityThread.java:4627) 
      07-03 08:01:09.230: E/AndroidRuntime(32523): at java.lang.reflect.Method.invokeNative(Native Method) 
      07-03 08:01:09.230: E/AndroidRuntime(32523): at java.lang.reflect.Method.invoke(Method.java:521) 
      07-03 08:01:09.230: E/AndroidRuntime(32523): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858) 
      07-03 08:01:09.230: E/AndroidRuntime(32523): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
      07-03 08:01:09.230: E/AndroidRuntime(32523): at dalvik.system.NativeStart.main(Native Method) 
      07-03 08:01:09.230: E/AndroidRuntime(32523): Caused by: java.lang.IllegalStateException 
      07-03 08:01:09.230: E/AndroidRuntime(32523): at android.media.MediaRecorder.start(Native Method) 
      07-03 08:01:09.230: E/AndroidRuntime(32523): at com.webvideo.com.MainActivity.onCreate(MainActivity.java:71) 
      07-03 08:01:09.230: E/AndroidRuntime(32523): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 
      07-03 08:01:09.230: E/AndroidRuntime(32523): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627) 

回答

0

就像您的代碼所說的,前置攝像頭的API是在Android 2.3(薑餅)中引入的。

它不會在Froyo上工作。