title: Cannot get camera preview (call camera) , HTC Thunderbolt
body: <p>I am new to android app development so please any answers would be great with code examples.</p>
  
  <p>My problem is this, I have to develop a camera app that streams mjpeg to a server.</p>
  
  <p>Now I have successfully created the app for my Motorola Atrix and that works great. My problem is after I signed and exported that app and installed it on an HTC Thunderbolt for additional device testing, I found that it freezes when I try to call for the camera and the app crashes.</p>
  
  <p>Since then I have tried every example/sample code for camera preview on the web and in these forums especially and I constantly get the same results. I get it to work on my Atrix but not on the thunderbolt.</p>
  
  <p>When the app runs on the thunderbolt I get this:</p>
  
  <pre><code>07-01 16:11:12.643: D/AndroidRuntime(1140): Shutting down VM
  07-01 16:11:12.643: W/dalvikvm(1140): threadid=1: thread exiting with uncaught exception (group=0x40015560)
  07-01 16:11:12.653: E/AndroidRuntime(1140): FATAL EXCEPTION: main
  07-01 16:11:12.653: E/AndroidRuntime(1140): java.lang.RuntimeException: Fail to connect to camera service
  07-01 16:11:12.653: E/AndroidRuntime(1140):     at android.hardware.Camera.native_setup(Native Method)
  07-01 16:11:12.653: E/AndroidRuntime(1140):     at android.hardware.Camera.&lt;init&gt;(Camera.java:258)
  07-01 16:11:12.653: E/AndroidRuntime(1140):     at android.hardware.Camera.open(Camera.java:235)
  07-01 16:11:12.653: E/AndroidRuntime(1140):     at vcore.java.MainActivity.surfaceCreated(MainActivity.java:95)
  07-01 16:11:12.653: E/AndroidRuntime(1140):     at android.view.SurfaceView.updateWindow(SurfaceView.java:543)
  07-01 16:11:12.653: E/AndroidRuntime(1140):     at android.view.SurfaceView.dispatchDraw(SurfaceView.java:348)
  07-01 16:11:12.653: E/AndroidRuntime(1140):     at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
  07-01 16:11:12.653: E/AndroidRuntime(1140):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
  07-01 16:11:12.653: E/AndroidRuntime(1140):     at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
  07-01 16:11:12.653: E/AndroidRuntime(1140):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
  07-01 16:11:12.653: E/AndroidRuntime(1140):     at android.view.View.draw(View.java:6883)
  07-01 16:11:12.653: E/AndroidRuntime(1140):     at android.widget.FrameLayout.draw(FrameLayout.java:357)
  07-01 16:11:12.653: E/AndroidRuntime(1140):     at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
  07-01 16:11:12.653: E/AndroidRuntime(1140):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
  07-01 16:11:12.653: E/AndroidRuntime(1140):     at android.view.View.draw(View.java:6883)
  07-01 16:11:12.653: E/AndroidRuntime(1140):     at android.widget.FrameLayout.draw(FrameLayout.java:357)
  07-01 16:11:12.653: E/AndroidRuntime(1140):     at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:1862)
  07-01 16:11:12.653: E/AndroidRuntime(1140):     at android.view.ViewRoot.draw(ViewRoot.java:1522)
  07-01 16:11:12.653: E/AndroidRuntime(1140):     at android.view.ViewRoot.performTraversals(ViewRoot.java:1258)
  07-01 16:11:12.653: E/AndroidRuntime(1140):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1859)
  07-01 16:11:12.653: E/AndroidRuntime(1140):     at android.os.Handler.dispatchMessage(Handler.java:99)
  07-01 16:11:12.653: E/AndroidRuntime(1140):     at android.os.Looper.loop(Looper.java:123)
  07-01 16:11:12.653: E/AndroidRuntime(1140):     at android.app.ActivityThread.main(ActivityThread.java:3683)
  07-01 16:11:12.653: E/AndroidRuntime(1140):     at java.lang.reflect.Method.invokeNative(Native Method)
  07-01 16:11:12.653: E/AndroidRuntime(1140):     at java.lang.reflect.Method.invoke(Method.java:507)
  07-01 16:11:12.653: E/AndroidRuntime(1140):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
  07-01 16:11:12.653: E/AndroidRuntime(1140):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
  07-01 16:11:12.653: E/AndroidRuntime(1140):     at dalvik.system.NativeStart.main(Native Method)
  </code></pre>
  
  <p>I emailed HTC themselves and they said they couldn't help me at all so I figured I would try my luck here.</p>
  
  <p>Does anyone know how to properly call the camera (for a camera preview) for an HTC Thunderbolt? </p>
  
  <p>If you could provide code example I would be eternally grateful.</p>
  
  <p>Just some examples of the things I have tried are:</p>
  
  <p><a href="https://stackoverflow.com/a/4798958/1195751">https://stackoverflow.com/a/4798958/1195751</a></p>
  
  <p><a href="https://stackoverflow.com/q/4998205/1195751">HTC Desire HD not accepts setParameter() with hardware.Camera</a>   This also didn't work</p>
  
  <p><a href="https://stackoverflow.com/q/4727957/1195751">Android: Camera Preview Orientation on HTC EVO (Android 2.1 or 2.2)</a>   Neither Did this</p>
  
  <p>I have really tried everything to solve this but haven't gotten anything to work so please, any solution to this problem would be extremely helpful.</p>
  
  <p><strong>UPDATE</strong> I already have the permissions set in my manifest</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="vcore.java"
      android:versionCode="1"
      android:versionName="1.0" &gt;
  
      &lt;uses-sdk android:minSdkVersion="8" /&gt;
  
      &lt;uses-permission android:name="android.permission.INTERNET"&gt;&lt;/uses-permission&gt;
      &lt;uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"&gt;&lt;/uses-permission&gt;
      &lt;uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"&gt;&lt;/uses-permission&gt;
      &lt;uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION"&gt;&lt;/uses-permission&gt;
      &lt;uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"&gt;&lt;/uses-permission&gt;
      &lt;uses-permission android:name="android.permission.RECORD_AUDIO"&gt;&lt;/uses-permission&gt;
      &lt;uses-permission android:name="android.permission.CAMERA"&gt;&lt;/uses-permission&gt;
      &lt;uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"&gt;&lt;/uses-permission&gt;
      &lt;uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS"&gt;&lt;/uses-permission&gt;
      &lt;uses-feature android:name="android.hardware.camera" /&gt;
      &lt;uses-feature android:name="android.hardware.camera.autofocus" /&gt;
      &lt;uses-permission android:name="android.permission.FLASHLIGHT"
      android:permissionGroup="android.permission-group.HARDWARE_CONTROLS"
      android:protectionLevel="normal"/&gt;
      &lt;uses-feature android:name="android.hardware.camera.flash" /&gt;
      &lt;uses-permission android:name="android.permission.ACCESS_WIFI_STATE"&gt;&lt;/uses-permission&gt;
      &lt;uses-permission android:name="android.permission.CHANGE_COMPONENT_ENABLED_STATE"&gt;&lt;/uses-permission&gt;
      &lt;uses-permission android:name="android.permission.RECORD_VIDEO"&gt;&lt;/uses-permission&gt; 
      &lt;uses-permission android:name="android.permission.WAKE_LOCK" /&gt;
  
      &lt;supports-screens
          android:largeScreens="true"   
          android:normalScreens="true"
          android:smallScreens="true"
          android:anyDensity="true" /&gt;
  
      &lt;application
          android:icon="@drawable/logo"
          android:label="@string/app_name" &gt;
          &lt;activity
              android:label="@string/app_name"
              android:name=".MainActivity"
              android:screenOrientation="portrait" &gt;
              &lt;intent-filter &gt;
  
                  &lt;action android:name="android.intent.action.MAIN" /&gt;
                  &lt;category android:name="android.intent.category.LAUNCHER" /&gt;
  
              &lt;/intent-filter&gt;
          &lt;/activity&gt;
  
          &lt;activity android:name=".PushCamera"
                    android:label="@string/app_name"&gt;
  
              &lt;!--   android:screenOrientation="portrait"  --&gt;
          &lt;/activity&gt;
           &lt;activity android:name="ImageDisplayActivity"&gt;&lt;/activity&gt;
      &lt;/application&gt;
  
  &lt;/manifest&gt;
  </code></pre>
  
  <p><strong>UPDATE 2</strong></p>
  
  <pre><code>public void surfaceCreated(SurfaceHolder holder) {
          try {
              if (mCamera != null) {
                  try {
                      mCamera.stopPreview();
                  } catch (Exception ignore) {
                  }
                  try {
                      mCamera.release();
                  } catch (Exception ignore) {
                  }
                  mCamera = null;
              }
  
              mCamera = Camera.open();
              mCamera.setPreviewDisplay(holder);
          } catch (Exception ex) {
              try {
                  if (mCamera != null) {
                      try {
                          mCamera.stopPreview();
                      } catch (Exception ignore) {
                      }
                      try {
                          mCamera.release();
                      } catch (Exception ignore) {
                      }
                      mCamera = null;
                  }
              } catch (Exception ignore) {
  
              }
          }
      }
  
      public void surfaceDestroyed(SurfaceHolder holder) {
          try {
              if (mCamera != null) {
                  try {
                      mCamera.stopPreview();
                  } catch (Exception ignore) {
                  }
                  try {
                      mCamera.release();
                  } catch (Exception ignore) {
                  }
                  mCamera = null;
              }
          } catch (Exception ex) {
              ex.printStackTrace();
          }
      }
  
      public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
      Log.e(TAG, "surfaceChanged");
      try {
          Camera.Parameters parameters = mCamera.getParameters();
          try {
              List&lt;Camera.Size&gt; supportedSizes = null;
              //On older devices (&lt;1.6) the following will fail
              //the camera will work nevertheless
              supportedSizes = parameters.getSupportedPreviewSizes();
  
              //preview form factor
              float ff = (float)w/h;
              Log.d("Mixare", "Screen res: w:"+ w + " h:" + h + " aspect ratio:" + ff);
  
              //holder for the best form factor and size
              float bff = 0;
              int bestw = 0;
              int besth = 0;
              Iterator&lt;Camera.Size&gt; itr = supportedSizes.iterator();
  
              //we look for the best preview size, it has to be the closest to the
              //screen form factor, and be less wide than the screen itself
              //the latter requirement is because the HTC Hero with update 2.1 will
              //report camera preview sizes larger than the screen, and it will fail
              //to initialize the camera
              //other devices could work with previews larger than the screen though
              while(itr.hasNext()) {
                  Camera.Size element = itr.next();
                  //current form factor
                  float cff = (float)element.width/element.height;
                  //check if the current element is a candidate to replace the best match so far
                  //current form factor should be closer to the bff
                  //preview width should be less than screen width
                  //preview width should be more than current bestw
                  //this combination will ensure that the highest resolution will win
                  Log.d("Mixare", "Candidate camera element: w:"+ element.width + " h:" + element.height + " aspect ratio:" + cff);
                  if ((ff-cff &lt;= ff-bff) &amp;&amp; (element.width &lt;= w) &amp;&amp; (element.width &gt;= bestw)) {
                      bff=cff;
                      bestw = element.width;
                      besth = element.height;
                  }
              } 
              Log.d("Mixare", "Chosen camera element: w:"+ bestw + " h:" + besth + " aspect ratio:" + bff);
              //Some Samsung phones will end up with bestw and besth = 0 because their minimum preview size is bigger then the screen size.
              //In this case, we use the default values: 480x320
              if ((bestw == 0) || (besth == 0)){
                  Log.d("Mixare", "Using default camera parameters!");
                  bestw = 480;
                  besth = 320;
              }
              parameters.setPreviewSize(bestw, besth);
          } catch (Exception ex) {
              parameters.setPreviewSize(480 , 320);
          }
  
          mCamera.setParameters(parameters);
          mCamera.startPreview();
      } catch (Exception ex) {
          ex.printStackTrace();
      }
  
  }
  </code></pre>