title: Camera on Android Example
body: <p>I want to write an activity that:</p>
  
  <ol>
  <li>Shows the camera preview (viewfinder), and has a "capture" button.</li>
  <li>When the "capture" button is pressed, takes a picture and returns it to the calling activity (setResult() &amp; finish()).</li>
  </ol>
  
  <p>Are there any <strong>complete</strong> examples out there that works on <strong>every device</strong>? A link to a simple open source application that takes pictures would be the ideal answer.</p>
  
  <hr>
  
  <p><strong>My research so far:</strong></p>
  
  <p>This is a common scenario, and there are many questions and tutorials on this.</p>
  
  <p>There are two main approaches:</p>
  
  <ol>
  <li>Use the android.provider.MediaStore.ACTION_IMAGE_CAPTURE event. See <a href="https://stackoverflow.com/questions/3442462/how-to-capture-an-image-and-store-it-with-the-native-android-camera">this question</a></li>
  <li>Use the Camera API directly. See <a href="http://marakana.com/forums/android/examples/39.html" rel="nofollow noreferrer">this example</a> or <a href="https://stackoverflow.com/questions/5752570/camera-in-android">this question (with lots of references)</a>.</li>
  </ol>
  
  <p>Approach 1 would have been perfect, but the issue is that the intent is implemented differently on each device. <del>On some devices it works well. However, on some devices you can take a picture but it is never returned to your app. On some devices nothing happens when you launch the intent.</del> Typically it also saves the picture to the SD card, and requires the SD card to be present. The user interaction is also different on every device.</p>
  
  <p>With approach 2 the issues is stability. I tried some examples, but I've managed to stop the camera from working (until a restart) on some devices and completely freeze another device. On another device the capture worked, but the preview stayed black.</p>
  
  <p>I would have used ZXing as an example application (I work with it a lot), but it only uses the preview (viewfinder), and doesn't take any pictures. I also found that on some devices, ZXing did not automatically adjust the white balance when the lighting conditions changed, while the native camera app did it properly (not sure if this can be fixed).</p>
  
  <hr>
  
  <p><strong>Update:</strong></p>
  
  <p>For a while I used the camera API directly. This gives more control (custom UI, etc), but I would not recommend it to anyone. I would work on 90% of devices, but every now and again a new device would be released, with a different problem.</p>
  
  <p>Some of the problems I've encountered:</p>
  
  <ul>
  <li>Handling autofocus</li>
  <li>Handling flash</li>
  <li>Supporting devices with a front camera, back camera or both</li>
  <li>Each device has a different combination of screen resolution, preview resolutions (doesn't always match the screen resolution) and picture resolutions.</li>
  </ul>
  
  <p>So in general, I'd not recommend going this route at all, unless there is no other way. After two years I dumped by custom code and switched back to the Intent-based approach. Since then I've had much less trouble. The issues I've had with the Intent-based approach in the past was probably just my own incompetence.</p>
  
  <p>If you <em>really</em> need to go this route, I've heard it's much easier if you only support devices with Android 4.0+.</p>