title: How do I take a photo programmatically
body: <p>I'm developing an app which get image whenever server call and send that image to server. Here is some code in <code>CameraSurfaceView</code> class which I define.</p>
  
  <pre><code>private SurfaceHolder holder;
  private Camera camera;        
  private Bitmap osb;
  public void takeImage(){
      Camera camera = this.getCamera();
      CameraSurfaceView.HandlePictureStorage c = this.new HandlePictureStorage();
      camera.takePicture(null, null, c);
  }
  public byte[] getImage(String type) {    
      if (osb==null) {
          System.err.println("Camera.getImage: osb NULL");
          return  null;
      }
  
      CompressFormat format = CompressFormat.JPEG;
      if (type.equals("PNG")) {
          format = CompressFormat.PNG;
      }
  
      ByteArrayOutputStream out = new ByteArrayOutputStream();
      try {
          System.err.println("Camera.getImage: decoded");
          osb.compress(format, 100, out);
          System.err.println("Camera.getImage: compressed");
          try {
              out.close();
          } catch (IOException e) {
              e.printStackTrace();
          }
      } finally {
          //lock.unlock();
      }
  
      byte[] bytes = out.toByteArray();
      return bytes;
  }
  
  
  public void saveImage(byte[] picture) {
      System.err.println("saveImage");
      osb = BitmapFactory.decodeByteArray(picture, 0, picture.length);
  }
  </code></pre>
  
  <p>and inner class <code>HandlePictureStorage</code> implements <code>PictureCallBack</code></p>
  
  <pre><code>@Override
  public void onPictureTaken(byte[] picture, Camera camera) 
  {                    
      CameraSurfaceView.this.saveImage(picture);
  }
  </code></pre>
  
  <p>when I call <code>takeImage()</code> and then <code>getImage()</code> function from outer , i expected that the code on <code>onPictureTaken(byte[] picture, Camera camera)</code> of class <code>HandlePictureStorage</code> will be executed right after <code>takeImage()</code>  to save image and before <code>getImage()</code> so <code>getImage()</code> have data to retrieve. </p>
  
  <hr>
  
  <p>Unfortunately, when i see on the logcat, the <code>getImage()</code> function run right after <code>takeImage()</code> and before <code>onPictureTaken()</code>. hence, it's obvious that <code>takeImage</code> can only get null data because there is no picture was saved before.
  if run <code>takeImage()</code> and <code>getImage()</code> again, function <code>getImage()</code> will return the picture of previous shot.</p>