3

How can I send take a picture command to the in-built (default) camera app?

Suppose the camera app is running in the foreground. Now, instead of tapping on the on-screen camera button I would like my background service ask the camera app to take a picture. How can this be achieved?

I understand the possibility of sending an intent to the camera app, but that would then need a tap on the on-screen button to take a picture, or creating my own custom camera app which can then be controlled. But I am looking for a way (legal or hack) to make the in-built camera app receptive to my take a picture command.

We have these phones with physical camera buttons, pressing which triggers something and convey a message to the camera app to take a picture. Can I somehow trigger that something from my background service?

abbas
  • 6,453
  • 2
  • 40
  • 36
  • check my question for what I tried to achieve and my answer on how I made it work.. http://stackoverflow.com/questions/12995185/android-taking-photos-and-saving-them-with-a-custom-name-to-a-custom-destinati – Razgriz Oct 20 '14 at 01:57
  • Your solution requires a tap on the on-screen button. – abbas Oct 20 '14 at 22:40
  • 2
    I don't understand why you would want to do this. Why do you want to use the default camera app if you are going to ignore it? And why bother using the camera app if you just want to automatically take a picture? http://developer.android.com/training/camera/cameradirect.html – mattm Oct 22 '14 at 12:29
  • because I want to store the picture in the gallery where the default camera app stores its pictures. And I don't want to create my own camera app. – abbas Oct 22 '14 at 12:43
  • 1
    You can use UIAutomation script and code. http://developer.android.com/tools/help/uiautomator/index.html – DearDhruv Oct 29 '14 at 06:04
  • Is the device rooted? Or maybe it's a custom ROM? – Alex Cohn Oct 29 '14 at 13:29
  • Non-root solution will be better. – abbas Oct 29 '14 at 13:35

3 Answers3

6

How can I send take a picture command to the in-built (default) camera app?

There are dozens of "in-built (default) camera apps". Device manufacturers usually ship their own.

How can I achieve this?

There is no requirement that any camera app, let alone an "in-built" one, support any sort of API that allows third-party apps to control the camera behavior, let alone offer the specific access that you are describing.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
2

You have asked to take control on the built in camera app function of taking picture, which is not possible!

And to do this sort of functionality, you should create your own camera app, which listens your command and behave accordingly.There are dozens of tutorials for custom camera app available on internet, one of them is this. I hope it will help you.

Hamad
  • 5,096
  • 13
  • 37
  • 65
  • I wonder how those phones with "physical" camera button convey a message to the camera app to take a picture. Can this communication be somehow intercepted or hacked? – abbas Oct 29 '14 at 13:34
2

You wont be able to achieve that with the in built app as CommonsWare said.

But you must be able to use intent to dispatch a capture:

 static final int REQUEST_IMAGE_CAPTURE = 1;

   private void dispatchTakePictureIntent() {
       Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
       if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
          startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
       }
}

for this you will also have to add the below feature to your app:

<uses-feature android:name="android.hardware.camera" android:required="true" />

you can read more on android developers' website.

EDIT

public void capture(){

    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");
  }
}

You can call the Capture() function on any event in you background app. This should be working

Community
  • 1
  • 1
vedbhawsar
  • 75
  • 8
  • but this starts the in-built camera activity and would demand a tap on the on-screen camera button to take a picture. – abbas Oct 29 '14 at 13:24
  • Again, it just starts the camera activity and does not take a picture unless you tap on the button. – abbas Nov 12 '14 at 21:01