Created
April 2, 2017 20:15
-
-
Save iamtodor/1c7e7be9e492e74449c37fc45d97335e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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