116

I'm connecting a webcam to my emulator by setting the front camera to "webcam0" in the AVD Manager. When I start the emulator's camera application, I get the error

CameraService::connect X (pid 702) rejected (invalid cameraId 0).

Here's the relevant portion of the Android source code:

sp<ICamera> CameraService::connect(
    const sp<ICameraClient>& cameraClient, int cameraId) {

    int callingPid = getCallingPid();

    [...]

    if (cameraId < 0 || cameraId >= mNumberOfCameras) {
        LOGE("CameraService::connect X (pid %d) rejected (invalid cameraId %d).",
            callingPid, cameraId);
        return NULL;
    }

    [...]
}

The webcam has been correctly assigned an ID of 0 because there's only 1 camera. However, mNumberOfCameras is presumably still 0. This means that the camera is being registered by the emulator, but it hasn't bothered to update the number of connected cameras.

How can I connect a webcam so that it will be properly recognized by the emulator?

Edit: the command emulator -webcam-list -avd <name of your AVD> in \android-sdks\tools gives the result:

List of web cameras connected to the computer:
Camera `webcam0` is connected to device `AndroidEmulatorVC0` on channel 0 using pixel format `BGR4`

When I launch the webcam from Eclipse's AVD manager or using emulator -camera-front webcam0 -avd <name of your AVD>, I get the following window:

enter image description here

Edit 2: This seems to be a bug in the emulator. The suggested answer tells you what to do to set up the camera, but doesn't solve the problem for me. I ultimately solved it by using a laptop with a built-in webcam. Perhaps another USB webcam might have worked as well.

1''
  • 26,823
  • 32
  • 143
  • 200

3 Answers3

172

UPDATE

In Android Studio AVD:

  1. Open AVD Manager:

AVD menu

  1. Add/Edit AVD:

Specific AVD strip

  1. Click Advanced Settings in the bottom of the screen:

AVD Advanced Settings

  1. Set your camera of choice as the front/back cameras:

AVD Camera Settings

nurnachman
  • 4,468
  • 2
  • 37
  • 40
  • 5
    You should't set the front and back camera's to the same source. – Chris River Oct 14 '18 at 20:52
  • 2
    @ChrisRiver Why not? It shouldn't matter. – DaveTheMinion Jan 27 '19 at 18:36
  • 2
    Cool! But how to change webcam if we have more than one? – walkmn Feb 26 '19 at 09:54
  • 1
    If the Camera app crashes with the error "Can't connect to the camera", try rebooting the emulated device and clearing the Camera's app's cache & data. This worked for me. – Joseph238 May 09 '19 at 16:18
  • Sometimes it is helpful to set front and back camera's to same source, like when Camera isn't recognizing a webcam as a valid input source (i.e. when it only shows the other source and doesn't let you invert to the other camera) – Joseph238 May 09 '19 at 16:21
  • 9
    It instantly crashes simulator when the camera starts. I'm on a mac. – Milind Mevada May 12 '20 at 05:11
  • @MilindMevada, after a few days and a few laptop restarts, I noticed it works, and my face/ webcam feed is showing live on the emulator, sideways though. – Ben Butterworth May 25 '20 at 20:10
  • ok thanks. I had a permission issue that's why it was crashing. – Milind Mevada May 28 '20 at 08:42
  • 3
    @walkmn You can use alternate cam via the CLI: `cd "${ANDROID_HOME}/tools"; ./emulator -avd $VIRTUAL_DEVICE_AVD_ID -camera-back webcam1` – steamer25 Jun 21 '20 at 16:38
  • @MilindMevada make sure you allow the android emulator to use your mac hardware camera - if you didn't allow in the prompt on first try, you can set it in the privacy settings of your mac in system preferences. thanks for the highlight. – nurnachman Nov 12 '20 at 07:47
  • See also https://stackoverflow.com/questions/36832637/android-studios-emulator-has-an-error-cant-connect-to-camera/36904976 which fixed the follow-up error for me – user985366 Jan 02 '21 at 14:08
  • Attention!!! : the front and back cameras should not be the same!!! – Guilherme Abacherli Aug 01 '22 at 19:51
47

Follow the below steps in Eclipse.

  1. Goto -> AVD Manager
  2. Create/Edit the AVD.
  3. Hardware > New:
  4. Configures camera facing back
  5. Click on the property value and choose = "webcam0".
  6. Once done all the above the webcam should be connected. If it doesnt then you need to check your WebCam drivers.

Check here for more information : How to use web camera in android emulator to capture a live image?

enter image description here

Community
  • 1
  • 1
Lalith B
  • 11,843
  • 6
  • 29
  • 47
  • 2
    I think this is an older UI for the AVD manager. I used to have a window that looked like this, but now I can select the front and rear cameras from dedicated drop-down menus. – 1'' Jan 02 '13 at 16:01
  • I use a mac so eclipse looks this way in mac osx. So the windows and menus might be different in Microsoft windows and linux os. – Lalith B Jan 02 '13 at 17:41
  • Note that you also need to choose correct API. You can choose emulation for API 10 but it won't work. I think API 16 was the first to really support it. – Nux Jun 23 '13 at 01:54
  • No the emulation is based on ADT and not on Android API. ADT supports emulation since 19 I think. – Lalith B Jun 23 '13 at 14:26
  • In the Android Studio, for the step 3 select the "Show Advanced Settings" button to display the camera options. – eldes Sep 02 '16 at 13:15
4

On macOS

Explanation

Adding to @nurnachman's answer above, on macOS you'll also need to grant the emulator elevated permissions by launching it from the Terminal using sudo.

Steps

1. Use the Android Device Manager to set the Camera settings to Webcam0

UPDATE

In Android Studio AVD:

  1. Open AVD Manager:

AVD menu

  1. Add/Edit AVD:

Specific AVD strip

  1. Click Advanced Settings in the bottom of the screen:

AVD Advanced Settings

  1. Set your camera of choice as the front/back cameras:

AVD Camera Settings

2. Launch the macOS Terminal

3. In the Terminal, enter the following command to list the names of your installed Android Emulators

Emulators Installed via Android Studio:

~/Library/Android/sdk/emulator/emulator -list-avds

Emulators Installed via Visual Studio for Mac:

~/Library/Developer/Xamarin/android-sdk-macosx/emulator emulator -list-avds

4. Launch the Android Emulator with elevated privileges:

Emulators Installed via Android Studio:

sudo ~/Library/Android/sdk/emulator/emulator -avd [Your Emulator Name]

Note: Replace [Your Emulator Name] with the name of your emulator discovered in Step 3

e.g. sudo ~/Library/Android/sdk/emulator/emulator -avd pixel_5_-api_33

Emulators Installed via Visual Studio for Mac:

~/Library/Developer/Xamarin/android-sdk-macosx/emulator -avd [Your Emulator Name]

Note: Replace [Your Emulator Name] with the name of your emulator discovered in Step 3

e.g. sudo ~/Library/Developer/Xamarin/android-sdk-macosx/emulator -avd pixel_5_-api_33

5. When the Emulator launches and when your app access the Camera, accept macOS request to grant Camera permissions


Troubleshooting

Ensure Camera Permission Enabled for Terminal

If the Android Camera is still not working after launching it with elevated permissions via the Terminal, in System Settings.app, navigate to Privacy & Security -> Camera and ensure the Camera Permission for Terminal is enabled

enter image description here

Brandon Minnick
  • 13,342
  • 15
  • 65
  • 123
  • Unfortunately I don't see terminal in this Camera permission tab – hannes ach Jan 30 '23 at 13:26
  • @hannesach The option won't appear in System Settings under the Camera's Permissions tab until you've set the Android Emulator to use `webcom0`, then used Terminal to launch the Android Emulator and subsequently launch the camera on the Android Emulator. The steps to accomplish this are all listed above. – Brandon Minnick Jan 30 '23 at 19:54
  • I use MacOS Venture 13.1, I set Android Emulator to use `webcom0` but I never see terminal in System settings. That's why I'm asking – hannes ach Jan 31 '23 at 05:47
  • @hannesach Yes. You must first launch the Emulator from the terminal with elevated privileges (sudo) as described in the steps above. I can confirm the above steps work on macOS 13.1. – Brandon Minnick Feb 01 '23 at 01:35
  • 1
    I did a start from terminal as well `~/Library/Android/sdk/emulator/emulator -avd API_30_Google_Api -camera-front webcam0`same with all combinations of `-camera-back`, `-camera-front`, `webcam1` and `webcam0`. Even when I set it in UI settings. It never recognize a camera and I'm never asked from MacOS. So it must be something else. Btw, I use a Apple Silicon M1 – hannes ach Feb 01 '23 at 07:24