闪光灯相机崩溃

时间:2014-03-29 20:18:24

标签: android flash camera

我的应用程序使用相机闪光灯,但我不明白为什么如果我打开另一个应用程序然后恢复我的应用程序应用程序崩溃。我想因为我没有在应用程序中插入暂停和恢复。

这是MainActivity:

public class MainActivity extends ActionBarActivity {

//flag to detect flash is on or off
    private boolean isLighOn = false;

    private Camera camera;

    private Button button;

    private LinearLayout rl;

    int i=0;

    @Override
    protected void onStop() {
        super.onStop();

        if (camera != null) {
            camera.release();
        }
    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        button = (Button) findViewById(R.id.buttonFlashlight);
        rl=(LinearLayout) findViewById(R.id.container);
        rl.setBackgroundColor(getResources().getColor(R.color.Black)); 


        Context context = this;
        PackageManager pm = context.getPackageManager();

        // if device support camera?
        if (!pm.hasSystemFeature(PackageManager.FEATURE_CAMERA)) {
            Log.e("err", "Device has no camera!");
            return;
        }

        camera = Camera.open();
        final Parameters p = camera.getParameters();

        button.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {

                if (isLighOn) {

                    Log.i("info", "torch is turn off!");

                    p.setFlashMode(Parameters.FLASH_MODE_OFF);
                    camera.setParameters(p);
                    camera.stopPreview();
                    isLighOn = false;
                    button.setBackgroundResource(R.drawable.off);

                    rl.setBackgroundColor(getResources().getColor(R.color.Black)); 

                } else {

                    Log.i("info", "torch is turn on!");

                    p.setFlashMode(Parameters.FLASH_MODE_TORCH);

                    camera.setParameters(p);
                    camera.startPreview();
                    isLighOn = true;
                    button.setBackgroundResource(R.drawable.on);

                    rl.setBackgroundResource(R.drawable.check);

                }

            }
        });

    }

@Override
public boolean onCreateOptionsMenu(Menu menu) {

    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}
}

logcat的:

03-29 21:24:51.048: D/AndroidRuntime(27113): Shutting down VM
03-29 21:24:51.048: W/dalvikvm(27113): threadid=1: thread exiting with uncaught exception (group=0x418eeda0)
03-29 21:24:51.058: E/AndroidRuntime(27113): FATAL EXCEPTION: main
03-29 21:24:51.058: E/AndroidRuntime(27113): Process: com.example.torcia, PID: 27113
03-29 21:24:51.058: E/AndroidRuntime(27113): java.lang.RuntimeException: Method called after release()

2 个答案:

答案 0 :(得分:0)

当您让应用进入后台时,会调用stop()。然后你释放你的相机。在此之后你不能再使用它 - 你需要调用camera.open()并获得一个新的相机对象。我建议不要在onCreate中创建相机对象,而只在onStart(或onResume)中创建它。

答案 1 :(得分:0)

问题是您在应用程序停止时释放了相机,但在恢复应用程序时不会再次初始化相机。你的相机初始化代码在onCreate中,不会再被调用。

最好在onResume中初始化相机并在onPause中释放它。

E.g。

@Override
protected void onResume() {
    super.onResume();

    camera = Camera.open();
}

@Override
protected void onPause() {
    super.onPause();

    if (camera != null) {
        camera.release();
    }
}