Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Camera control inside an Android application

I am building an application for Android 2.2 which is based on a photo library. There is an option to take picture from camera and use it in the application. I am expecting:

  1. To enable the camera from the application.

  2. To take the picture.

  3. To automatically close the camera and show the captured picture inside the application

Unfortunately, I am not able to get the captured image to the application. Once photo is taken, camera is not closing automatically or returning to the app. Now I have to click the back button to go to the app and select the picture manually select from SD card. Camera is opening through intent and I am using the following class.

http://developer.android.com/reference/android/hardware/Camera.html

like image 261
Deepu S Nath Avatar asked Sep 24 '26 15:09

Deepu S Nath


1 Answers

You need to do two things. Start the Camera app and tell it where to store the picture that it takes:

File photo = new File(Environment.getExternalStorageDirectory(),  "myFile.jpg");
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo));
startActivityForResult(intent, myRequestCode);

When the user closes the Camera app, you app and Activity will be resumed. So you must override onActivityResult to get the result:

if (resultCode == myRequestCode){
  File photo = new File(Environment.getExternalStorageDirectory(), "myFile.jpg");
  // open it, show it, insert into MediaStore whatever
}

If you don't provide the place to save, you can alternatively retrieve it using either intent.getData or intent.getParcelableExtra using Intent.EXTRA_STREAM.

like image 168
michaelg Avatar answered Sep 26 '26 05:09

michaelg



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!