Camera start when a button is pressed in android

I want to open the device’s camera when we press a button in our application. Please help me.

+5
source share
5 answers

Inside the onClick button,

 Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
 startActivityForResult(intent, 0); 

And add the ability to use Camers in your manifest file.

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

See additional discussion here Camera target for Android

+17
source

First you need to filter out the system application, and then you can check the camera’s activity, I’ll just answer a similar question here .

0
source

` BtnSelectImage.setOnClickListener(new Button.OnClickListener()
         {
             @Override
             public void onClick(View v)
             {
                 startCamera();
                 Intent intent=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                 intent.putExtra(MediaStore.EXTRA_OUTPUT,
                 MediaStore.Images.Media.EXTERNAL_CONTENT_URI.getPath());
                 startActivityForResult(intent, 1);
             }
        });                                                                                             `
0
source
#initialize in main activity 
    path = Environment.getExternalStorageDirectory()
            + "/images/make_machine_example.jpg"; #
    ImageView image=(ImageView)findViewById(R.id.image);
 //--------------------------------------------------||

 public void FromCamera(View) {

    Log.i("camera", "startCameraActivity()");
    File file = new File(path);
    Uri outputFileUri = Uri.fromFile(file);
    Intent intent = new Intent(
            android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
    intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
    startActivityForResult(intent, 1);

}

public void FromCard() {
    Intent i = new Intent(Intent.ACTION_PICK,
            android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    startActivityForResult(i, 2);
}

 protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == 2 && resultCode == RESULT_OK
            && null != data) {

        Uri selectedImage = data.getData();
        String[] filePathColumn = { MediaStore.Images.Media.DATA };

        Cursor cursor = getContentResolver().query(selectedImage,
                filePathColumn, null, null, null);
        cursor.moveToFirst();

        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
        String picturePath = cursor.getString(columnIndex);
        cursor.close();

        bitmap = BitmapFactory.decodeFile(picturePath);
        image.setImageBitmap(bitmap);

        if (bitmap != null) {
            ImageView rotate = (ImageView) findViewById(R.id.rotate);

        }

    } else {

        Log.i("SonaSys", "resultCode: " + resultCode);
        switch (resultCode) {
        case 0:
            Log.i("SonaSys", "User cancelled");
            break;
        case -1:
            onPhotoTaken();
            break;

        }

    }

}

protected void onPhotoTaken() {
    // Log message
    Log.i("SonaSys", "onPhotoTaken");
    taken = true;
    imgCapFlag = true;
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inSampleSize = 4;
    bitmap = BitmapFactory.decodeFile(path, options);
    image.setImageBitmap(bitmap);


}
0
source

This works (in the onCreateView fragment lifecycle method):

    Button button1 = (Button) view.findViewById(R.id.button1);
    button1.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            startActivity(new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE));
        }
    });
-2
source

All Articles