1

アクティビティがバックグラウンドになったときにカメラを解放するにはどうすればよいですか? (例:電話機のスタンバイボタンを押す)

    public void onPause(){
    super.onPause();
    if (cam != null) {
        cam.stopPreview();
        cam.release();
        cam = null;
    }
}

@Override
protected void onResume(){
 super.onResume();
    rLayout = (RelativeLayout) findViewById (R.id.layout_background);
    cam = Camera.open();  
    cam.startPreview();
    p = cam.getParameters();
}

LogCat:

02-20 11:46:40.040: E/AndroidRuntime(26439): FATAL EXCEPTION: main
02-20 11:46:40.040: E/AndroidRuntime(26439): java.lang.RuntimeException: Unable to resume activity {spicysoftware.spicytaschenlampe/spicysoftware.spicytaschenlampe.FlashLight}: java.lang.RuntimeException: Fail to connect to camera service
02-20 11:46:40.040: E/AndroidRuntime(26439):    at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2616)
02-20 11:46:40.040: E/AndroidRuntime(26439):    at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2644)
02-20 11:46:40.040: E/AndroidRuntime(26439):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2130)
02-20 11:46:40.040: E/AndroidRuntime(26439):    at android.app.ActivityThread.access$600(ActivityThread.java:140)
02-20 11:46:40.040: E/AndroidRuntime(26439):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
02-20 11:46:40.040: E/AndroidRuntime(26439):    at android.os.Handler.dispatchMessage(Handler.java:99)
02-20 11:46:40.040: E/AndroidRuntime(26439):    at android.os.Looper.loop(Looper.java:137)
02-20 11:46:40.040: E/AndroidRuntime(26439):    at android.app.ActivityThread.main(ActivityThread.java:4898)
02-20 11:46:40.040: E/AndroidRuntime(26439):    at java.lang.reflect.Method.invokeNative(Native Method)
02-20 11:46:40.040: E/AndroidRuntime(26439):    at java.lang.reflect.Method.invoke(Method.java:511)
02-20 11:46:40.040: E/AndroidRuntime(26439):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1008)
02-20 11:46:40.040: E/AndroidRuntime(26439):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:775)
02-20 11:46:40.040: E/AndroidRuntime(26439):    at dalvik.system.NativeStart.main(Native Method)
02-20 11:46:40.040: E/AndroidRuntime(26439): Caused by: java.lang.RuntimeException: Fail to connect to camera service
02-20 11:46:40.040: E/AndroidRuntime(26439):    at android.hardware.Camera.native_setup(Native Method)
02-20 11:46:40.040: E/AndroidRuntime(26439):    at android.hardware.Camera.<init>(Camera.java:353)
02-20 11:46:40.040: E/AndroidRuntime(26439):    at android.hardware.Camera.open(Camera.java:330)
02-20 11:46:40.040: E/AndroidRuntime(26439):    at spicysoftware.spicytaschenlampe.FlashLight.onResume(FlashLight.java:182)
02-20 11:46:40.040: E/AndroidRuntime(26439):    at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1199)
02-20 11:46:40.040: E/AndroidRuntime(26439):    at android.app.Activity.performResume(Activity.java:5280)
02-20 11:46:40.040: E/AndroidRuntime(26439):    at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2606)
02-20 11:46:40.040: E/AndroidRuntime(26439):    ... 12 more

これがその仕組みです:

    public void onPause(){
    super.onPause();
    Log.v("OnPause", "True");
    if (cam != null) {
        Log.v("OnPause", "Cam isn't null");
        cam.stopPreview();
        Log.v("OnPause", "CamPreview stopped");
        cam.release();
        Log.v("OnPause", "CamReleased");
        cam = null;
        Log.v("OnPause", "Cam is null");
    }
}


@Override
protected void onResume(){
 super.onResume();
 Log.v("OnResume", "True");
    if(cam != null){
        Log.v("OnResume", "Camera is in use");
    } else {
        Log.v("OnResume", "Camera isn't in use");
        rLayout = (RelativeLayout) findViewById (R.id.layout_background);
        cam = Camera.open();  
        Log.v("OnResume", "Camera has been opened");
        cam.startPreview();
        Log.v("OnResume", "CamPreview started");
        p = cam.getParameters();
        Log.v("OnResume", "Wrote parameters to variable p");
    }
}

onResume でカメラを開くと、アプリがクラッシュします。

よろしくMSeiz5

4

2 に答える 2

1

onResume でカメラを再度開いてリセットします。

@Override
protected void onResume(){
 super.onResume();
 camera = Camera.open();

 // We resumed so lock the camera
 Camera.Parameters params = camera.getParameters();

 params.setFlashMode(Parameters.FLASH_MODE_ON);
 //params.setFocusMode(Parameters.FOCUS_MODE_AUTO);
 //params.setWhiteBalance(Parameters.WHITE_BALANCE_AUTO);
 params.setJpegQuality(100);

 camera.setParameters(params);
 ImageView overlay = (ImageView) findViewById(R.id.ImageView01);
 overlay.bringToFront();
}
于 2013-02-20T09:34:41.980 に答える