Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Unregister camera button

Tags:

android

camera

I tried to bind some actions to a camera button:

videoPreview.setOnKeyListener(new OnKeyListener() {
    public boolean onKey(View v, int keyCode, KeyEvent event) {
        if(event.getAction() == KeyEvent.ACTION_DOWN) {
            switch(keyCode) {
                case KeyEvent.KEYCODE_CAMERA:
                    //videoPreview.onCapture(settings);
                    onCaptureButton();
...
            }
        }
        return false;
    }
});

Pressing the button however the application crashes because the original Camera application starts.

Does anyone know how to prevent Camera application start when the camera button is pressed?

like image 354
Niko Gamulin Avatar asked Jun 12 '09 13:06

Niko Gamulin


People also ask

How do I lock my Android camera?

To turn off the camera of your Android smartphone, go to Settings > Apps > Camera app > Permissions > Disable camera.


1 Answers

In your example you need to return true to let it know you "consumed" the event. Like this:

videoPreview.setOnKeyListener(new OnKeyListener(){
    public boolean onKey(View v, int keyCode, KeyEvent event){
        if(event.getAction() == KeyEvent.ACTION_DOWN) {
            switch(keyCode) {
                case KeyEvent.KEYCODE_CAMERA:
                    //videoPreview.onCapture(settings);
                    onCaptureButton();
                    /* ... */
                    return true;
            }
        }
        return false;
    }
});

It will also only work if the videoPreview (or a child element) has focus. So you could either set it to have focus by default:

@Override
public void onResume() {
    /* ... */
    videoPreview.requestFocus();
    super.onResume();
}

or (prefered) put the listener on the top-level element (eg. a LinearLayout, RelativeLayout, etc).

like image 64
Jeremy Logan Avatar answered Oct 10 '22 14:10

Jeremy Logan