我正在开发一个相机应用程序。调用后何时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 的 android.hardware.Camera.(Camera.java:421) 的 android.hardware.Camera.native_setup(Native Method) 的相机服务。打开(Camera.java:382) 在 com.joltatech.videowatermark.VideoCaptureActivity$17.doInBackground(VideoCaptureActivity.java:770) 在 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) 在 java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) 在 java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) 在 java.lang.Thread.run(Thread.java :841)第587章)第587章)
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);
}