我正在尝试从平板电脑的前置摄像头捕获视频并运行2.2 Android Froyo。我真的无法弄清楚这个错误背后的原因。由于平板电脑是Tegra 2 Nvigia芯片组,它可能会导致问题,是平板电脑有前置摄像头。请任何人都可以给我一个方向休息我会弄清楚。
这是我正在使用的代码,
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 :(得分:0)
就像你的代码所说,前置摄像头的API是在Android 2.3(姜饼)中引入的。
它不适用于Froyo。