Skip to content

Instantly share code, notes, and snippets.

@jsam
Created September 19, 2013 17:05
Show Gist options
  • Save jsam/6626531 to your computer and use it in GitHub Desktop.
Save jsam/6626531 to your computer and use it in GitHub Desktop.
saving async task
/**
* Async class for communication with I/O for saving taken photo
* @param jpeg byte representation of given photo for saving to filesystem
*/
class SavePhotoTask extends AsyncTask<byte[], String, String> {
@Override
protected String doInBackground(byte[]... jpeg) {
UUID image_name = UUID.randomUUID();
File photo = new File(Environment.getExternalStorageDirectory(),
PICTURES_DIR + image_name.toString());
if (photo.exists()) {
photo.delete();
}
try {
FileOutputStream fos = new FileOutputStream(photo.getPath());
fos.write(jpeg[0]);
fos.close();
}
catch (java.io.IOException e) {
Log.e("PictureDemo", "Exception in photoCallback", e);
}
return(null);
}
}
if (imageBytes != null) {
new SavePhotoTask().execute(imageBytes);
Toast.makeText(getApplicationContext(),
"Saving photo to Images folder.",
Toast.LENGTH_LONG);
}
else {
Toast.makeText(getApplicationContext(),
"There's nothing to save",
Toast.LENGTH_LONG);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment