title: Flow of events in Android
body: <p>I'm new to developing on android, and I'm finding the flow of events quite confusing.</p>
  
  <p>I'm trying to develop a simple app that will take a photo every 3 seconds and save it to external storage. The (really bad) code that I wrote looks like it should be following these sequence of events:</p>
  
  <p><code>button pressed &gt; take photo &gt; save photo &gt; take photo &gt; save photo &gt; ...</code> </p>
  
  <p>but instead it seems to be doing the following:</p>
  
  <p><code>button pressed &gt; take photo &gt; ... &gt; take photo &gt; save photo &gt; ... &gt; save photo</code></p>
  
  <p>i.e. saving all the photos in RAM presumably, then writing them all to the disk all at the end, instead of on the way. This fails for large amounts of images.</p>
  
  <p>Can anyone point me in the right direction? Here is my (awful) code:</p>
  
  <pre><code>@SuppressWarnings("deprecation")
  public class PhotoActivity extends Activity {
  
      Camera c = Camera.open();
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_photo);
  
          // Original, eh?
          final Button button = (Button)findViewById(R.id.button1);
  
          button.setOnClickListener(new View.OnClickListener() {
              public void onClick(View v) {
                  try {
                      // Fails for large i
                      for(int i = 0; i &lt; 500; i++) {
                          // I don't think this is right.
                          Thread.sleep(1500);
                          takePictureNoPreviewAtAll(i);
                      }
                  } catch (IOException e) {
                      e.printStackTrace();
                  } catch (InterruptedException e) {
                      e.printStackTrace();
                  }
              }
          });
      }
  
      private void takePictureNoPreviewAtAll(int i) throws IOException, InterruptedException {
          // I don't know why this is 1, but it seems to work :)
          SurfaceTexture mTexture = new SurfaceTexture(1);
          c.setPreviewTexture(mTexture);
          c.startPreview();
  
          // This is not right either, but the camera seems to need both sleeps to work.
          Thread.sleep(1500);
          c.takePicture(null, null, getJpegCallback(i));
      }
      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
          getMenuInflater().inflate(R.menu.menu_photo, menu);
          return true;
      }
  
      private android.hardware.Camera.PictureCallback getJpegCallback(final int i) {
          android.hardware.Camera.PictureCallback jpeg = new android.hardware.Camera.PictureCallback() {
              @Override
              public void onPictureTaken(byte[] data, Camera camera) {
                  try {
                      // Provided in a separate file, just saving to SD card, it seems to work.
                      Map&lt;String, File&gt; externalLocations = ExternalStorage.getAllStorageLocations();
                      File externalSdCard = externalLocations.get(ExternalStorage.EXTERNAL_SD_CARD);
  
                      FileOutputStream f = new FileOutputStream(externalSdCard.getPath() + "/" + System.currentTimeMillis() + ".jpg");
                      f.write(data);
                      f.flush();
                      f.close();
                      Log.d(externalSdCard.getAbsolutePath(), "ddd");
                  } catch (IOException e) {
                      //do something about it
                      e.printStackTrace();
                  }
              }
          };
          return jpeg;
      }
  }
  </code></pre>
  
  <p>If needed, I can provided the source of <code>ExternalStorage</code>.</p>