I have a requirement where i have to store the last 10 preview frames of android camera when the capture button is clicked. I am able to put 10 frames into a buffer and when the capture button is clicked i am cloning this buffer and saving the frames to file system through an AsyncTask. Everything is working fine, but the problem is with activity life cycle. When i press home or menu button while the AsyncTask is still executing, my AsyncTask stops and i can only see 3 to 5 frames in the file system. I have already tried many alternatives like doing it in a Thread, ExecutorService and even an Intent Service. The problem with using the service is that my buffer contains data more than 10mb and the limit for activity-service communication is 500kb to 1mb(i think, as i got an exception when tried). Below is my code, any help is much appreciated.
private class PreviewFramesProcessor extends AsyncTask<List<byte[]>, Void, Void> {
@Override
protected void onPreExecute() {
dialog.setIndeterminate(true);
dialog.setCanceledOnTouchOutside(false);
dialog.setCancelable(false);
dialog.setMessage("Processing...");
dialog.show();
}
@Override
protected Void doInBackground(List<byte[]>...temp) {
/*String folder = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).getAbsolutePath();
panImagesFolder = new File(folder + File.separator + getUniqueFolderName());*/
panImagesFolder.mkdirs();
List<byte[]> byteArrayList = temp[0];
for(int i = byteArrayList.size() - 1; i >= 0 ; i--) {
File pictureFile = getUniqueMediaFileUri(panImagesFolder, i);
try {
byte[] tempArray = convertYuvToJpeg(byteArrayList.get(i), mCameraDevice);
FileOutputStream fos = new FileOutputStream(pictureFile);
fos.write(tempArray);
fos.close();
galleryAddPic(pictureFile);
byteArrayList.remove(i);
} catch(Exception e) {
Log.e("Nagendra", "Exception in processByteArrayBuffer(): " + e.getMessage());
}
}
return null;
}
@Override
protected void onPostExecute(Void result) {
isPictureTaken = false;
dialog.hide();
}
@Override
protected void onProgressUpdate(Void... values) {
}
}