gpt4 book ai didi

安卓摄像机 : NullPointerException

转载 作者:行者123 更新时间:2023-11-29 18:14:10 30 4
gpt4 key购买 nike

我正在开发一个 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.unlock(); 之前使用 mCamera = Camera.open(0); 并在设备上测试它,而不是在模拟器上。

关于安卓摄像机 : NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9035766/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com