Fail to connect to camera service on G1
<p>I apologize in advance for this long post, but it's necessary to show what is happening.</p>

<p>I've been working on an app that works on the 1.6 emulator, but bombs on my G1.</p>

<p>Here is the Photographer activity:</p>

<pre><code>public class Photographer extends Activity {

 private SurfaceView preview = null;
 private SurfaceHolder previewHolder = null;
 private Camera camera = null;
 public static final int IMAGE_HEIGHT = 320;
 public static final int IMAGE_WIDTH = 480;

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

  preview = (SurfaceView)findViewById(R.id.preview);
  previewHolder = preview.getHolder();
  previewHolder.addCallback(surfaceCallback);
  previewHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
 }

 private void takePicture() {
  camera.stopPreview();
  camera.takePicture(null, null, photoCallback);
 }

 SurfaceHolder.Callback surfaceCallback = new SurfaceHolder.Callback() {
  public void surfaceCreated(SurfaceHolder holder) {
   camera = Camera.open();

   try {
    camera.setPreviewDisplay(previewHolder);
   }
   catch (Throwable t) {
    Log.e("Photographer", "Exception in setPreviewDisplay()", t);
   }
   try {
    takePicture();
   } catch (Throwable t) {
    Log.e("Photographer", "Exception in takePicture()", t);
   }
  }

  public void surfaceChanged(SurfaceHolder holder,
         int format, int width, int height) {
   Camera.Parameters parameters=camera.getParameters();
   parameters.setPreviewSize(IMAGE_WIDTH, IMAGE_HEIGHT);
   parameters.setPictureFormat(PixelFormat.JPEG);
   camera.setParameters(parameters);
   camera.startPreview();
  }

  public void surfaceDestroyed(SurfaceHolder holder) {
   camera.stopPreview();
   camera.release();
   camera = null;
  }
 };

 Camera.PictureCallback photoCallback = new Camera.PictureCallback() {
  public void onPictureTaken(byte[] data, Camera camera) {
   new SavePhotoTask().execute(data);
   camera.startPreview();
  }
 };

 class SavePhotoTask extends AsyncTask&lt;byte[], String, String&gt; {
  @Override
  protected String doInBackground(byte[]... jpeg) {
   File photo = new File(Environment.getExternalStorageDirectory(), "WIIA-new.jpg");
   if (photo.exists())
    photo.renameTo(new File(Environment.getExternalStorageDirectory(), "WIIA-old.jpg"));

   try {
    FileOutputStream fos = new FileOutputStream(photo.getPath());

    fos.write(jpeg[0]);
    fos.close();
    Log.e("Photographer", "Picture taken");
    // returning result
      Intent returnIntent = new Intent();
      setResult(RESULT_OK, returnIntent);     
      finish();
   }
   catch (java.io.IOException e) {
    Log.e("Photographer", "Exception in photoCallback", e);

    // returning result
      Intent returnIntent = new Intent();
    setResult(RESULT_CANCELED, returnIntent);
    finish();
   }

   return(null);
  }
 }

}
</code></pre>

<p>and here is where this activity gets called from:</p>

<pre><code>    public class WIIA extends Activity {

 public class CountDown extends CountDownTimer {
  private TextView tvCountDown = (TextView) findViewById(R.id.progress);
  private TextView tvSystemState = (TextView) findViewById(R.id.system_state);

  public CountDown(long millisInFuture, long countDownInterval) {
   super(millisInFuture, countDownInterval);
  }

  @Override
  public void onFinish() {
   tvSystemState.setText(R.string.system_state_armed);
   tvCountDown.setText(R.string.armed);
   snapshotInterval = Long.parseLong(settings.getString("snapshot_interval_list", "5"));
   imgCaptureTimer = new Timer("imageCapture");
   imgCaptureTimer.scheduleAtFixedRate(doRefresh, 0, snapshotInterval*1000);
  }

  @Override
  public void onTick(long millisUntilFinished) {
   tvCountDown.setText(millisUntilFinished/1000 + " seconds until Armed");
  }

 }

 private TimerTask doRefresh = new TimerTask() {
  public void run() {
   if (armed) {
    refreshImage();
   } else {
    imgCaptureTimer.cancel();
   }
  }
 };

 private void refreshImage() {
  Thread updateThread = new Thread(null, backgroundCaptureImage, "capture_image");  
  updateThread.start();        
 }

 private Runnable backgroundCaptureImage = new Runnable() {
  public void run() {
   doRefreshImage();
  }        
 };

 private void doRefreshImage() {
  Intent photographer = new Intent(this, Photographer.class);
  startActivityForResult(photographer, GET_PHOTO);
 }

 @Override
 public void onActivityResult(int requestCode, int resultCode, Intent data) {
  super.onActivityResult(requestCode, resultCode, data);
  switch(requestCode) {
   case (GET_PHOTO) : {
    if(resultCode == RESULT_OK) {
    // go on to compare images
     new ProcessImageTask().execute();
    }
    break;
   }
  }
 }
</code></pre>

<p>Having trouble pasting the relevant portions of AndroidManifest.xml, but I have permissions for Camera:</p>

<pre><code>  &lt;uses-permission android:name="android.permission.CAMERA" /&gt;
</code></pre>

<p>here's the Photographer part:</p>

<pre><code>      &lt;activity android:name=".Photographer"
   android:configChanges="keyboardHidden|orientation"
   android:screenOrientation="landscape"
   android:theme="@android:style/Theme.NoTitleBar.Fullscreen"&gt;
  &lt;/activity&gt;
</code></pre>

<p>also set for testing on G1:</p>

<pre><code>android:debuggable="true"
</code></pre>

<p>and working off of 1.6:</p>

<pre><code>&lt;uses-sdk android:minSdkVersion="4" /&gt;
</code></pre>

<p>All this works on a 1.6 emulator, but when I run this on the G1, LogCat shows:</p>

<pre><code>INFO/ActivityManager(74): Starting activity: Intent { cmp=com.androidsecurity.wiia/.Photographer }

WARN/WindowManager(74): App freeze timeout expired.

WARN/WindowManager(74): Force clearing freeze: AppWindowToken{432e1520 token=HistoryRecord{43124c10 com.androidsecurity.wiia/.Photographer}}

WARN/ActivityManager(74): Activity pause timeout for HistoryRecord{43124c10 com.androidsecurity.wiia/.Photographer}

ERROR/QualcommCameraHardware(51): native_get_picture: MSM_CAM_IOCTL_GET_PICTURE fd 13 error Connection timed out

ERROR/QualcommCameraHardware(51): getPicture failed!

DEBUG/QualcommCameraHardware(51): snapshot_thread X

INFO/QualcommCameraHardware(51): initPreview E: preview size=480x320

DEBUG/QualcommCameraHardware(51): frame_thread E

DEBUG/CameraService(51): CameraService::connect E (pid 1031, client 0x3b510)

DEBUG/CameraService(51): CameraService::connect X (pid 1031, new client 0x3b510) rejected. (old pid 1031, old client 0x3b2e0)

DEBUG/AndroidRuntime(1031): Shutting down VM

WARN/dalvikvm(1031): threadid=3: thread exiting with uncaught exception (group=0x4001da28)

ERROR/AndroidRuntime(1031): Uncaught handler: thread main exiting due to uncaught exception

ERROR/AndroidRuntime(1031): java.lang.RuntimeException: Fail to connect to camera service

ERROR/AndroidRuntime(1031):     at android.hardware.Camera.native_setup(Native Method)

ERROR/AndroidRuntime(1031):     at android.hardware.Camera.native_setup(Native Method)

ERROR/AndroidRuntime(1031):     at android.hardware.Camera.open(Camera.java:67)

ERROR/AndroidRuntime(1031):     at com.androidsecurity.wiia.Photographer$1.surfaceCreated(Photographer.java:78)

ERROR/AndroidRuntime(1031):     at android.view.SurfaceView.updateWindow(SurfaceView.java:392)

ERROR/AndroidRuntime(1031):     at android.view.SurfaceView.dispatchDraw(SurfaceView.java:264)

ERROR/AndroidRuntime(1031):     at android.view.ViewGroup.drawChild(ViewGroup.java:1524)

ERROR/AndroidRuntime(1031):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1256)

ERROR/AndroidRuntime(1031):     at android.view.ViewGroup.drawChild(ViewGroup.java:1524)

ERROR/AndroidRuntime(1031):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1256)

ERROR/AndroidRuntime(1031):     at android.view.View.draw(View.java:6277)

ERROR/AndroidRuntime(1031):     at android.widget.FrameLayout.draw(FrameLayout.java:352)

ERROR/AndroidRuntime(1031):     at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:1883)

ERROR/AndroidRuntime(1031):     at android.view.ViewRoot.draw(ViewRoot.java:1332)

ERROR/AndroidRuntime(1031):     at android.view.ViewRoot.performTraversals(ViewRoot.java:1097)

ERROR/AndroidRuntime(1031):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1613)

ERROR/AndroidRuntime(1031):     at android.os.Handler.dispatchMessage(Handler.java:99)

ERROR/AndroidRuntime(1031):     at android.os.Looper.loop(Looper.java:123)

ERROR/AndroidRuntime(1031):     at android.app.ActivityThread.main(ActivityThread.java:4203)

ERROR/AndroidRuntime(1031):     at java.lang.reflect.Method.invokeNative(Native Method)

ERROR/AndroidRuntime(1031):     at java.lang.reflect.Method.invoke(Method.java:521)

ERROR/AndroidRuntime(1031):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)

ERROR/AndroidRuntime(1031):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:549)

ERROR/AndroidRuntime(1031):     at dalvik.system.NativeStart.main(Native Method)
</code></pre>

<p>and /data/anr/traces.txt shows:</p>

<pre><code>`DALVIK THREADS:
"main" prio=5 tid=3 NATIVE
  | group="main" sCount=1 dsCount=0 s=N obj=0x4001db08 self=0xbc48
  | sysTid=358 nice=0 sched=0/0 handle=-1343996920
  at android.os.BinderProxy.transact(Native Method)
  at android.app.ActivityManagerProxy.handleApplicationError(ActivityManagerNative.java:2243)
  at com.android.internal.os.RuntimeInit.crash(RuntimeInit.java:302)
  at com.android.internal.os.RuntimeInit$UncaughtHandler.uncaughtException(RuntimeInit.java:75)
  at java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:887)
  at java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:884)
  at dalvik.system.NativeStart.main(Native Method)

"imageCapture" prio=5 tid=17 TIMED_WAIT
  | group="main" sCount=1 dsCount=0 s=N obj=0x432a66f0 self=0x1dc820
  | sysTid=366 nice=0 sched=0/0 handle=1938080
  at java.lang.Object.wait(Native Method)
  - waiting on &lt;0x1c8cd8&gt; (a java.util.Timer$TimerImpl)
  at java.lang.Object.wait(Object.java:326)
  at java.util.Timer$TimerImpl.run(Timer.java:250)

"Binder Thread #3" prio=5 tid=15 NATIVE
  | group="main" sCount=1 dsCount=0 s=N obj=0x4327aa38 self=0x160ea0
  | sysTid=365 nice=0 sched=0/0 handle=1406616
  at dalvik.system.NativeStart.run(Native Method)

"Binder Thread #2" prio=5 tid=13 NATIVE
  | group="main" sCount=1 dsCount=0 s=N obj=0x4327a978 self=0x1609c0
  | sysTid=364 nice=0 sched=0/0 handle=498392
  at dalvik.system.NativeStart.run(Native Method)

"Binder Thread #1" prio=5 tid=11 NATIVE
  | group="main" sCount=1 dsCount=0 s=N obj=0x432799d0 self=0x155268
  | sysTid=363 nice=0 sched=0/0 handle=1397288
  at dalvik.system.NativeStart.run(Native Method)

"JDWP" daemon prio=5 tid=9 VMWAIT
  | group="system" sCount=1 dsCount=0 s=N obj=0x432782a0 self=0x15f788
  | sysTid=361 nice=0 sched=0/0 handle=1614352
  at dalvik.system.NativeStart.run(Native Method)

"Signal Catcher" daemon prio=5 tid=7 RUNNABLE
  | group="system" sCount=0 dsCount=0 s=N obj=0x432781e8 self=0x18a0f0
  | sysTid=360 nice=0 sched=0/0 handle=1614000
  at dalvik.system.NativeStart.run(Native Method)

"HeapWorker" daemon prio=5 tid=5 VMWAIT
  | group="system" sCount=1 dsCount=0 s=N obj=0x422dd3e8 self=0x1177a8
  | sysTid=359 nice=0 sched=0/0 handle=1613728
  at dalvik.system.NativeStart.run(Native Method)`
</code></pre>

<p>It sounds like the problem is a timeout issue, but I am at a loss as to where I should be looking .... does anyone have a clue?</p>

<p>Thanks!</p>
