简体   繁体   中英

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

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 .

I guess .. you are doing similar to the below code

Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); // request code

        startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);

using startActivityForResult instead of startActivity.. whenever you are done with your camera.. press ok.. and you will come back to your activity.. then in your activity onActivityResult callback will be called. here you will get the data for the clicked image.

Thanks.

michaelg answers is spot on.

Only you have to consider Devices with the image capture bug.

Read about it here:

Android ACTION_IMAGE_CAPTURE Intent

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM