Skip to content

Instantly share code, notes, and snippets.

@iamtodor iamtodor/pic.java
Created Apr 2, 2017

Embed
What would you like to do?
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
You can’t perform that action at this time.