我正在开发相机应用程序。在调用OnResume()
后调用onPause()
时,它会出现以下异常:
04-07 17:46:35.374 3674-4562 / com.joltatech.videowatermark W / CameraBase:连接到相机时出错:0 04-07 17:46:35.374 3674-4562 / com.joltatech.videowatermark A / VideoCaptureActivity:无法获取相机 java.lang.RuntimeException:无法连接到摄像头服务 在android.hardware.Camera.native_setup(本机方法) 在android.hardware.Camera。(Camera.java:421) 在android.hardware.Camera.open(Camera.java:382) at com.joltatech.videowatermark.VideoCaptureActivity $ 17.doInBackground(VideoCaptureActivity.java:770) at com.joltatech.videowatermark.VideoCaptureActivity $ 17.doInBackground(VideoCaptureActivity.java:765) 在android.os.AsyncTask $ 2.call(AsyncTask.java:288) 在java.util.concurrent.FutureTask.run(FutureTask.java:237) 在android.os.AsyncTask $ SerialExecutor $ 1.run(AsyncTask.java:231) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) at java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:587) 在java.lang.Thread.run(Thread.java:841)
public static Camera checkForCamera(){
Camera camera = null;
try {
camera = Camera.open(); // this line will throw exception if camera is not in use.
}
catch (Exception e){
// if exception is thrown, return your boolean value here...
}
return camera; // if instance of camera, if it is not available it will return null.
}
void releaseCamera() {
if (this.camera != null) {
this.camera.lock(); // unnecessary in API >= 14
this.camera.stopPreview();
this.camera.release();
this.camera = null;
this.cameraPreviewFrame.removeView(this.cameraPreview);
}
}
void releaseMediaRecorder() {
if (this.mediaRecorder != null) {
this.mediaRecorder.reset(); // clear configuration (optional here)
this.mediaRecorder.release();
this.mediaRecorder = null;
}
}
void releaseResources() {
this.releaseMediaRecorder();
if(checkForCamera()!=null)
this.releaseCamera();
}
@Override
public void onPause() {
this.releaseResources();
super.onPause();
}
@Override
protected void onResume() {
super.onResume();
// initialize the camera in background, as this may take a while
new AsyncTask<Void, Void, Camera>() {
@Override
protected Camera doInBackground(Void... params) {
try {
Camera camera = Camera.open();
return camera == null ? Camera.open(0) : camera;
} catch (RuntimeException e) {
Log.wtf(TAG, "Failed to get camera", e);
return null;
}
}
@Override
protected void onPostExecute(Camera camera) {
if (camera == null) {
Toast.makeText(VideoCaptureActivity.this, "Cannot record video.",
Toast.LENGTH_SHORT);
} else {
VideoCaptureActivity.this.initCamera(camera);
}
}
}.execute();}
void initCamera(Camera camera) {
// we now have the camera
this.camera = camera;
Camera.Parameters params = camera.getParameters();
if (params.getSupportedFocusModes().contains(
Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO)) {
params.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO);
}
this.camera.setParameters(params);
// create a preview for our camera
this.cameraPreview = new CameraPreview(VideoCaptureActivity.this, this.camera);
// add the preview to our preview frame
this.cameraPreviewFrame.addView(this.cameraPreview, 0);
// enable just the record button
this.recordButton.setEnabled(true);
}