Using the camera from android [duplicated]

1

Developing an app, I've run into a doubt.

My intention is that by pressing a button, the camera is activated, and the photo, or the photos that are made, are redirected to a previously established folder.

I'm testing on Android 6, so I guess you have to apply for permits and such ...

Could someone show an example on the subject?

    
asked by Sergio Cv 04.08.2016 в 12:26
source

2 answers

1

Here is the official documentation with more examples. Basically to use the application of the camera that comes in the phone you should do the following:

// esto va en la declaracion de la clase, se usa en onActivityResult
static final int REQUEST_IMAGE_CAPTURE = 1234;

... // luego en algún botón pones esto

Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
    startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}

The activity that invokes this has to overload the method onActivityResult

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // verificar que vuelva de la camara
    if (requestCode == REQUEST_IMAGE_CAPTURE ) {
        // asegurarse que tomo la fotografia (pudo cancelar)
        if (resultCode == RESULT_OK) {
            Bundle extras = data.getExtras();
            Bitmap imageBitmap = (Bitmap) extras.get("data");
            // este image view muestra la fotografia.
            mImageView.setImageBitmap(imageBitmap);
        }
    }
}

Also you need to authorize the APP to use the camera, add this to the manifest.

<uses-feature android:name="android.hardware.camera" android:required="true" />
    
answered by 04.08.2016 в 14:15
1

The important thing in Android 6.0 is to request the permissions for the camera, for this you can use this method:

private void checkCameraPermission() {
    int permissionCheck = ContextCompat.checkSelfPermission(
            this, Manifest.permission.CAMERA);
    if (permissionCheck != PackageManager.PERMISSION_GRANTED) {
        Log.i("Mensaje", "No se tiene permiso para la Camara.");
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, 225);
    } else {
        Log.i("Mensaje", "Se tiene permiso para usar la camara!");
    }
}

Here is an example of how to open the camera: How to open a phone's camera from Android? and you can review details in the documentation.

What you would do is, from a button add a listener which activates the camera:

   Button botonCamera = (Button)findViewById(R.id.mybutton);
   botonCamera.setOnClickListener(new View.OnClickListener() {
       @Override
       public void onClick(View view) {
         try{
            mCamera = Camera.open();
        } catch (Exception e){
            Log.d("ERROR", "Failed to get camera: " + e.getMessage());
        }

        if(mCamera != null) {
            mCameraView = new CameraView(this, mCamera);
            FrameLayout camera_view = (FrameLayout)findViewById(R.id.camera_view);
            camera_view.addView(mCameraView);//agrega la vista CameraView()
         }
        }
    });

But if you want to save the photos in a specific path you can do it by trying:

   Button botonCamera = (Button)findViewById(R.id.mybutton);
   botonCamera.setOnClickListener(new View.OnClickListener() {
       @Override
       public void onClick(View view) {
       Uri uriSavedImage = Uri.fromFile(new File("/sdcard/mi_folder/mi_foto.png"));
camera.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);
startActivityForResult(camera, 1); 
       }
    });
    
answered by 04.08.2016 в 18:26