title: Can't Connect Camera using Android Camera (1) API on Raspberry Pi running AndroidThings
body: <p>I'm trying to run my standard Android app with QR code scanner on the Raspberry Pi running Android Things version 0.7 with the Raspberry Pi Camera V2 attached.</p>
  
  <p>The app and QR scanner runs fine on most Android phones (Lollipop+).</p>
  
  <p>On Android Things I can load the app and login fine, but when I open the QR scanner tab I get a crash. It's strange because the camera is detected (Camera.getNumberOfCameras() = 1) and the camera appears to have the front-facing orientation (cameraInfo.facing = 1), but when I try to open the camera i get RuntimeException: Fail to connect to camera service (see stacktrace below). Note that I open the camera on a background thread using Rx:</p>
  
  <pre><code>[C:CameraSource] [M:getIdForRequestedCamera] [L:836]: numberOfCameras = 1
  [C:CameraSource] [M:getIdForRequestedCamera] [L:839]: id 0, cameraInfo.facing = 1
  W/CameraBase: An error occurred while connecting to camera 0: Status(-8): '10: connectHelper:1399: Failed to initialize camera "0": No such device (-19)'
  W/System.err: io.reactivex.exceptions.OnErrorNotImplementedException: Fail to connect to camera service
  W/System.err:     at io.reactivex.internal.functions.Functions$OnErrorMissingConsumer.accept(Functions.java:704)
  W/System.err:     at io.reactivex.internal.functions.Functions$OnErrorMissingConsumer.accept(Functions.java:701)
  W/System.err:     at io.reactivex.internal.observers.ConsumerSingleObserver.onError(ConsumerSingleObserver.java:45)
  W/System.err:     at io.reactivex.internal.operators.single.SingleSubscribeOn$SubscribeOnObserver.onError(SingleSubscribeOn.java:73)
  W/System.err:     at io.reactivex.internal.operators.single.SingleFromCallable.subscribeActual(SingleFromCallable.java:43)
  W/System.err:     at io.reactivex.Single.subscribe(Single.java:2700)
  W/System.err:     at io.reactivex.internal.operators.single.SingleSubscribeOn$SubscribeOnObserver.run(SingleSubscribeOn.java:89)
  W/System.err:     at io.reactivex.Scheduler$DisposeTask.run(Scheduler.java:452)
  W/System.err:     at io.reactivex.internal.schedulers.ScheduledRunnable.run(ScheduledRunnable.java:61)
  W/System.err:     at io.reactivex.internal.schedulers.ScheduledRunnable.call(ScheduledRunnable.java:52)
  W/System.err:     at java.util.concurrent.FutureTask.run(FutureTask.java:266)
  W/System.err:     at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:301)
  W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
  W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
  W/System.err:     at java.lang.Thread.run(Thread.java:764)
  W/System.err: Caused by: java.lang.RuntimeException: Fail to connect to camera service
  W/System.err:     at android.hardware.Camera.&lt;init&gt;(Camera.java:519)
  W/System.err:     at android.hardware.Camera.open(Camera.java:365)
  </code></pre>
  
  <p>Also note that I'm using the old Android Camera (1) API.</p>
  
  <p>I came across a bug report where someone was trying to do the same thing running an Android app with Camera (1) API on AndroidThings/Rpi. They were able to connect to the same Raspberry Pi camera, but had some issues with the preview: <a href="https://issuetracker.google.com/issues/37130806" rel="nofollow noreferrer">https://issuetracker.google.com/issues/37130806</a>
  (More info <a href="https://plus.google.com/u/0/+PeterMeijer1/posts/1SKZGiP9jLr" rel="nofollow noreferrer">https://plus.google.com/u/0/+PeterMeijer1/posts/1SKZGiP9jLr</a> and <a href="https://plus.google.com/+RonaldVanDerLingen/posts/TE4JtJbHWqF" rel="nofollow noreferrer">https://plus.google.com/+RonaldVanDerLingen/posts/TE4JtJbHWqF</a>)</p>
  
  <p>Having played with Google Doorbell example app for AndroidThings, it appears that I might need to implement the Camera2 API. However, it looks like some have had success using the Camera (1) API.</p>
  
  <p>Most of all, I'd like to know why the camera is detected, but when I try to open it I get "No such device".</p>
  
  <p>Here's the code:</p>
  
  <pre><code>private static int getIdForRequestedCamera(int facing) {
      CameraInfo cameraInfo = new CameraInfo();
      for (int i = 0; i &lt; Camera.getNumberOfCameras(); ++i) {
          Camera.getCameraInfo(i, cameraInfo);
          if (cameraInfo.facing == facing) {
              return i;
          }
      }
      return -1;
  }
  
  private Camera createCamera() {
          int requestedCameraId = getIdForRequestedCamera(mFacing);
          if (requestedCameraId == -1) {
              throw new RuntimeException("Could not find requested 
  camera.");
              }
              Camera camera = Camera.open(requestedCameraId);
  ...
  }
  </code></pre>