Emre Sümer
Emre Sümer

Reputation: 11

How to launch camera after pressing a button in android studio?

I'm a beginner in android studio. I created a dialogue view and there's the button which proceeds to camera activation. I couldn't figure out a way to activate it. I just want to activate the camera right after pressing the button. Here are my codes:

public class AddPictureDialog extends Dialog {

    private Context context;

    private Camera camera;

    final Button takePhoto;

    private DB myDB;
    private SQLiteDatabase database;
    private Person person = new Person();

    public AddPictureDialog( Context context) {
        super(context);
        this.context = context;

        setCanceledOnTouchOutside(false);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.addimage_layout);

        camera = new Camera();


        takePhoto = (Button) findViewById(R.id.takenewphoto);
        takePhoto.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

                // Code here
            }
        });

    }
}

Upvotes: 1

Views: 15208

Answers (2)

kinezu
kinezu

Reputation: 1272

See the documentation here: http://developer.android.com/guide/topics/media/camera.html

First you are going to need to add camera permissions to your manifest file.

<uses-feature android:name="android.hardware.camera" android:required="false" />

If you want to store the picture, you will also need to add storage permissions.

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

The link also lists audio recording and location tagging permissions.

After that, follow these steps:

  1. Compose a Camera Intent - Create an Intent that requests an image or video, using one of these intent types:

    • MediaStore.ACTION_IMAGE_CAPTURE - Intent action type for requesting an image from an existing camera application.
    • MediaStore.ACTION_VIDEO_CAPTURE - Intent action type for requesting a video from an existing camera application.
  2. Start the Camera Intent - Use the startActivityForResult() method to execute the camera intent. After you start the intent, the Camera application user interface appears on the device screen and the user can take a picture or video.

  3. Receive the Intent Result - Set up an onActivityResult() method in your application to receive the callback and data from the camera intent. When the user finishes taking a picture or video (or cancels the operation), the system calls this method.

The provided link will give you examples.

Upvotes: 0

Gerard Rozsavolgyi
Gerard Rozsavolgyi

Reputation: 5074

First add this to your manifest file:

 <uses-feature android:name="android.hardware.camera"
              android:required="true" />

in order to use the camera and that if you use android less than 4.3, to be able to save the picture:

Then learn a little bit about Intents if you don't know yet. (An Intent basically let's you launch an external Activity)

Then following the instructions on android developper

Add these two fields:

private ImageView mImageView;
private Bitmap mImageBitmap;

and this method:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
        Bundle extras = data.getExtras();
        Bitmap imageBitmap = (Bitmap) extras.get("data");
        mImageView.setImageBitmap(imageBitmap);
    }
}

And this one to call when your Button is clicked.

private void handleSmallCameraPhoto(Intent intent) {
    Bundle extras = intent.getExtras();
    mImageBitmap = (Bitmap) extras.get("data");
    mImageView.setImageBitmap(mImageBitmap);
    mVideoUri = null;
    mImageView.setVisibility(View.VISIBLE);
    mVideoView.setVisibility(View.INVISIBLE);
}

Go to [http://developer.android.com/training/camera/photobasics.html][3] for more details and a full working example

Upvotes: 1

Related Questions