title: Camera SetPreviewCallback best way to convert YUV to RGB
body: <p>I am using this code to write an image to SD after <code>camera.takePicture</code> is called:</p>
  
  <pre><code>protected String doInBackground(byte[]... jpeg) {
      File directory=new File(Environment.getExternalStorageDirectory() + "/" + getString(R.string.qpw_picture_path) + "/" + getString(R.string.qpw_picture_title) + "_" + initialTime);
      directory.mkdirs();
      String currentTime = new SimpleDateFormat(getString(R.string.qpw_date_format)).format(new Date());
      File photo = new File (directory, getString(R.string.qpw_picture_title) + "_" + currentTime + "_" + current + ".jpg");
      current++;
  
      if (photo.exists()) {
          photo.delete();
      }
  
      try {
          FileOutputStream fos=new FileOutputStream(photo.getPath());         
          fos.write(jpeg[0]);
          fos.flush();
          fos.close();
      }
      catch (java.io.IOException e) {
      }
  
      new ImageMediaScanner(getBaseContext(), photo);
      return(null);
  }
  </code></pre>
  
  <p>Which is working fine in this case, but when I am using the same code to write images from <code>camera.setPreviewCallback</code>, I end up with 450KB corrupted images on SD which cannot be used or even opened.</p>
  
  <p>Any help or advice would be appreciated.</p>
  
  <p><strong>Edit:</strong> </p>
  
  <p>It seems that data should be first converted from YUV to RGB before saving. Having tried one of the many code samples found on Google and SO, I was facing no more issues.</p>
  
  <p>Does anyone know what is the best way of doing it? In terms of speed, memory allocation, CPU...</p>