Reputation: 988
I am developing an Android app to record videos with the help of Android developer guide. All the code on my programme is same as this page.
I have defined permission as this outside of the <application>
tag.
<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" />
When application starts the camera preview is working. But when I press recorde button, the NullPointerException is given. Exception:
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 line is mCamera.unlock();
Can you please let me know, what can be the reason for this (tested on Samsung galaxy s2 and Nexus s)? Thanks
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;
}
This is how my surface is created.
public void surfaceCreated(SurfaceHolder holder) {
try {
mCamera.setPreviewDisplay(holder);
mCamera.startPreview();
} catch (IOException e) {
Log.d(TAG, "Error setting camera preview: " + e.getMessage());
}
}
Upvotes: 1
Views: 2367
Reputation: 699
I know it's late, but it will help you.
Use mCamera = Camera.open(0);
before mcamera.unlock();
and test it on a device, not on emulator.
Upvotes: 2
Reputation: 2119
Try to releasing mCamera instance before call prepareVideoRecorder()
i.e. in surfaceCreated you use mCamera so I assume that you created an instance of Camera somewhere before. Then you are creating the second instance of Camera in prepareVideoRecorder() - the first line of that method is mCamera = getCameraInstance();
I believe that this is a cause of your problem. You must avoid creating more that one instance of Camera.
Upvotes: 1