Here we introduce how to use build-in camera to take a picture in both of Android and iOS devices.
Last active
October 24, 2016 03:25
-
-
Save pokk/680a963607fa48e2b30194b96e78030d to your computer and use it in GitHub Desktop.
Using build-in camera simplely
This file contains hidden or 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 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; | |
} | |
} | |
} |
This file contains hidden or 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
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