Skip to content

Instantly share code, notes, and snippets.

@pokk
Last active October 24, 2016 03:25
Show Gist options
  • Save pokk/680a963607fa48e2b30194b96e78030d to your computer and use it in GitHub Desktop.
Save pokk/680a963607fa48e2b30194b96e78030d to your computer and use it in GitHub Desktop.
Using build-in camera simplely
private static final int TAKE_PICTURE = 1;
private Uri imageUri;
public void takePhoto(View view) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// Keep the data from build-in camera path.
File file = new File(Environment.getExternalStorageDirectory() + File.separator + "image.jpg");
if (file.exists())
{
file.delete();
file = new File(Environment.getExternalStorageDirectory() + File.separator + "image.jpg");
}
imageUri = Uri.fromFile(photo);
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
startActivityForResult(intent, TAKE_PICTURE);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
// Camera request code.
case TAKE_PICTURE:
if (resultCode == RESULT_OK) {
Uri selectedImage = imageUri;
getContentResolver().notifyChange(selectedImage, null);
ImageView imageView = (ImageView) findViewById(R.id.ImageView);
try {
Bitmap bitmap = android.provider.MediaStore.Images.Media.getBitmap(getContentResolver(), selectedImage);
imageView.setImageBitmap(bitmap);
Toast.makeText(this, selectedImage.toString(), Toast.LENGTH_LONG).show();
} catch (Exception e) {
Toast.makeText(this, "Failed to load", Toast.LENGTH_SHORT).show();
Log.e("Camera", e.toString());
}
break;
}
}
}
class MyControlller UIViewController, UINavigationControllerDelegate, UIImagePickerControllerDelegate {
@IBOutlet weak var iv: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
let picker = UIImagePickerController()
picker.delegate = self
// Open the build-in camera.
picker.sourceType = .Camera
// Open the build-in photo roll.
// picker.sourceType = .PhotoLibrary
self.presentViewController(picker, animated: true, completion: nil)
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String:AnyObject]) {
// Set the picture you took to the image view.
self.iv.image = info[UIImagePickerControllerOriginalImage] as? UIImage
self.dismissViewControllerAnimated(true, completion: nil)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment