Skip to content

Instantly share code, notes, and snippets.

@marc0x71
Last active March 22, 2016 14:59
Show Gist options
  • Save marc0x71/6486405f79e692a9817d to your computer and use it in GitHub Desktop.
Save marc0x71/6486405f79e692a9817d to your computer and use it in GitHub Desktop.
Take photo from gallery and camera
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-feature android:name="android.hardware.camera"
android:required="true" />
protected File createImageFile() throws IOException {
// Create an image file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES);
File image = File.createTempFile(
imageFileName, /* prefix */
".jpg", /* suffix */
storageDir /* directory */
);
// Save a file: path for use with ACTION_VIEW intents
currentPhotoPath = image.getAbsolutePath();
return image;
}
private void takePhotoFromGallery() {
Log.d(TAG, "takePhotoFromGallery() called with: " + "");
Intent intent = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, REQUEST_IMAGE_GALLERY);
}
private void takePhotoFromCamera () {
Log.d(TAG, "takePhotoFromCamera() called with: " + "");
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
File photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException ex) {
// Error occurred while creating the File
ex.printStackTrace();
Toast.makeText(MainActivity.this, "Unable to create the photo file", Toast.LENGTH_SHORT).show();
}
// Continue only if the File was successfully created
if (photoFile != null) {
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.fromFile(photoFile));
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
} else {
Toast.makeText(MainActivity.this, "No camera found", Toast.LENGTH_SHORT).show();
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Bitmap imageBitmap = null;
Log.d(TAG, "onActivityResult() called with: " + "requestCode = [" + requestCode + "], resultCode = [" + resultCode + "], data = [" + data + "]");
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
Picasso.with(this).load(new File(currentPhotoPath)).into(imageView);
} else if (requestCode == REQUEST_IMAGE_GALLERY && resultCode == RESULT_OK) {
Uri selectedImage = data.getData();
Picasso.with(this).load(selectedImage).into(imageView);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment