0

カメラアプリを開発しています。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.(Camera.java:421) で android.hardware.Camera.(Camera.java:421) で android.hardware.Camera.native_setup(Native Method) でカメラ サービスに接続できません。 open(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) java.lang.Thread.run(Thread.java:841) で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);
}
4

0 に答える 0