Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android camera intent

I need to push an intent to default camera application to make it take a photo, save it and return an URI. Is there any way to do this?

like image 987
Alexander Oleynikov Avatar asked Apr 28 '10 12:04

Alexander Oleynikov


People also ask

What is camera intent?

The Android Camera application encodes the photo in the return Intent delivered to onActivityResult() as a small Bitmap in the extras, under the key "data" . The following code retrieves this image and displays it in an ImageView .

How do I open camera with intent?

Intent camera_intent = new Intent(MediaStore. ACTION_IMAGE_CAPTURE); startActivityForResult(camera_intent, pic_id); Now use onActivityResult() method to get the result, here the captured image.

What is camera API in Android?

This package is the primary API for controlling device cameras. It can be used to take pictures or videos when you are building a camera application. Camera. This class is the older deprecated API for controlling device cameras.


2 Answers

private static final int TAKE_PICTURE = 1;     private Uri imageUri;  public void takePhoto(View view) {     Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);     File photo = new File(Environment.getExternalStorageDirectory(),  "Pic.jpg");     intent.putExtra(MediaStore.EXTRA_OUTPUT,             Uri.fromFile(photo));     imageUri = Uri.fromFile(photo);     startActivityForResult(intent, TAKE_PICTURE); }  @Override public void onActivityResult(int requestCode, int resultCode, Intent data) {     super.onActivityResult(requestCode, resultCode, data);     switch (requestCode) {     case TAKE_PICTURE:         if (resultCode == Activity.RESULT_OK) {             Uri selectedImage = imageUri;             getContentResolver().notifyChange(selectedImage, null);             ImageView imageView = (ImageView) findViewById(R.id.ImageView);             ContentResolver cr = getContentResolver();             Bitmap bitmap;             try {                  bitmap = android.provider.MediaStore.Images.Media                  .getBitmap(cr, 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());             }         }     } } 
like image 96
Alexander Oleynikov Avatar answered Sep 22 '22 15:09

Alexander Oleynikov


Try the following I found here

Intent intent = new Intent("android.media.action.IMAGE_CAPTURE"); startActivityForResult(intent, 0);  @Override public void onActivityResult(int requestCode, int resultCode, Intent data) {   if (resultCode == Activity.RESULT_OK && requestCode == 0) {     String result = data.toURI();     // ...   } } 
like image 26
Ryan Avatar answered Sep 22 '22 15:09

Ryan