5

So i have a bluetooth device, i listen to button press on the device and i try to take a photo when the button is pressed. The issue is i didn't find any solution to do this.

} else if (destination.equals(APPLICATION_ACTION_DESTINATION_OPEN_CAMERA)) {
    Intent intent1 = new Intent("android.intent.action.CAMERA_BUTTON");
    intent1.putExtra("android.intent.extra.KEY_EVENT", new KeyEvent(0, KeyEent.KEYCODE_CAMERA));
    context.sendOrderedBroadcast(intent1, null);

    intent1 = new Intent("android.intent.action.CAMERA_BUTTON");
    intent1.putExtra("android.intent.extra.KEY_EVENT", new KeyEvent(1, KeyEvent.KEYCODE_CAMERA));
    context.sendOrderedBroadcast(intent1, null);
} else if (destination.equals(APPLICATION_ACTION_DESTINATION_TAKE_PHOTO)) {

}

The only way i found to do this is using:

Instrumentation.sendKeyDownUpSync();

The problems is i need to register INJECT_EVENTS permission which is only granted to System Apps.

Anyone managed to do this ?

Tazz
  • 781
  • 1
  • 8
  • 23
  • 1
    There are thousands of Android device models. These ship with hundreds of pre-installed camera apps, and there are hundreds more available for installation from the Play Store and elsewhere. None have to provide any means for outside apps to control when a picture is taken. – CommonsWare May 03 '16 at 14:11
  • I try to simulate the hardware volume up button, not for every app possible in short – Tazz May 03 '16 at 14:12
  • @Tazz Were you able to achieve it? I am facing a similar issue. – Prasad Rane Nov 01 '17 at 08:15
  • @Tazz I've also tried what you did before i found this. Have you found an alternative way? Please let us know. My attempt would now be to use root privileges to make the app itself a system app – WuerfelDev Nov 24 '17 at 21:43
  • @WuerfelDev no i didn't, feature got scratched – Tazz Jan 17 '18 at 13:16

1 Answers1

0

You can use this intent to take the pic

Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
startActivityForResult(cameraIntent, TAKE_PHOTO_CODE);

You can find further information here

and to invoke camera on volume key, all you need is to override this method in Activity

@Override
public boolean dispatchKeyEvent(KeyEvent event) {
    int keyCode = event.getKeyCode();
    switch (keyCode) {
    case KeyEvent.KEYCODE_VOLUME_UP:
    case KeyEvent.KEYCODE_VOLUME_DOWN:
        return true;
    default:
        return super.dispatchKeyEvent(event);
    }
}

I have used it, works flawless

Full Demo:

public class CameraDemoActivity extends Activity {
    int TAKE_PHOTO_CODE = 0;
    public static int count = 0;

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

        // Here, we are making a folder named Pitures to store
        // pics taken by the camera using this application

        final String dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + "/Pitures/";
        File newdir = new File(dir);
        newdir.mkdirs();

        Button capture = (Button) findViewById(R.id.btnCapture);
        capture.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

                // Here, the counter will be incremented each time, and the
                // picture taken by camera will be stored as 1.jpg,2.jpg and so on
                count++;
                String file = dir+count+".jpg";
                File newfile = new File(file);
                try {
                    newfile.createNewFile();
                }
                catch (IOException e)
                {
                }

                Uri outputFileUri = Uri.fromFile(newfile);

                Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);

                startActivityForResult(cameraIntent, TAKE_PHOTO_CODE);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == TAKE_PHOTO_CODE && resultCode == RESULT_OK) {
            Log.d("CameraDemo", "Pic saved");
        }
    }
}

SOURCE

Community
  • 1
  • 1
Atiq
  • 14,435
  • 6
  • 54
  • 69
  • I want to use the default camera app, not to make a different activity and open an instance of the camera :). Like i said, i want to simulate the volume up button which can be pressed inside most android camera apps to take a picture – Tazz May 03 '16 at 14:56
  • `android.media.action.IMAGE_CAPTURE"` requests image from the built in camera app – Atiq May 03 '16 at 15:07
  • have you checked the example that I have given reference of? – Atiq May 03 '16 at 15:09
  • Yes i've tried to send a KeyEvent.VOLUME_UP to the media, nothing happens :). I mean it only does volume up, no picture taken – Tazz May 03 '16 at 15:20
  • Anders, i don't need to invoke the camera on volume up key, i need to trigger Take photo on volume up key, that would have been like you said and its 100% correct, but not what im looking for – Tazz May 03 '16 at 18:24
  • it does take the picture as well you just needed to override `onActivityResult()` see my updated answer – Atiq May 03 '16 at 18:44
  • i know it can deliver the taken picture, but i fizically need to press the button for the photo, i need to simulate the hardware button to take a picture, not the picture itself, the result is not needed (the photo/video). I just need to take picture, start recording or stop recording, for that the start/take photo button needs to be pressed hardware :). In other words, its like pressing the button on the selfie stick, the picture its taken, and the result is stored, not needed for process. If you like we can go to chat to explain to you exactly. – Tazz May 04 '16 at 07:47