Skip to content

Instantly share code, notes, and snippets.

@iamtodor
Created April 2, 2017 20:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iamtodor/1c7e7be9e492e74449c37fc45d97335e to your computer and use it in GitHub Desktop.
Save iamtodor/1c7e7be9e492e74449c37fc45d97335e to your computer and use it in GitHub Desktop.
private void dispatchTakePictureIntent() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getContext().getPackageManager()) != null) {
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
}
@Override public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK) {
if (requestCode == REQUEST_IMAGE_CAPTURE) {
Bundle extras = data.getExtras();
Bitmap bitmap = (Bitmap) extras.get("data");
Uri path = Uri.fromFile(Utils.getFile(getContext(), bitmap));
listener.onImageChosen(path);
} else if (requestCode == REQUEST_IMAGE_PICK) {
Uri imageUri = data.getData();
listener.onImageChosen(imageUri);
}
dismiss();
}
}
@NonNull
public static File getFile(Context context, Bitmap bitmap) {
File file = new File(context.getCacheDir().getPath(), String.valueOf(System.currentTimeMillis()));
FileOutputStream out;
try {
if (!file.exists())
file.createNewFile();
out = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
out.close();
} catch (IOException e) {
e.printStackTrace();
}
return file;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment