I am using this code to write an image to SD after camera.takePicture
is called:
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);
}
Which is working fine in this case, but when I am using the same code to write images from camera.setPreviewCallback
, I end up with 450KB corrupted images on SD which cannot be used or even opened.
Any help or advice would be appreciated.
Edit:
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.
Does anyone know what is the best way of doing it? In terms of speed, memory allocation, CPU...