Android video app connect camera device

The android video app connect camera device tutorial describes how to make a connection to the device’s camera.

Patch for Manifest.permission.CAMERA issus

For those of you who have been having an issue with the Manifest.permission.CAMERA a patch is provided containing the fix.

I’ve provided the patch at github here .

And it just needs to be saved to your computer, I’ve provided a screenshot of my mac as an example

android git save permission patch

And here is a link to the tutorial describing how to apply patches.

Get Code

The code to start this tutorial is on github here

https://github.com/mobapptuts/android_camera2_api_video_app.git Tag camera2-video-camera-connect

or you can run this command

git clone https://github.com/mobapptuts/android_camera2_api_video_app.git –branch camera2-video-camera-connect

Steps

Add camera permission to the AndroidManifest.xml file

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

Inside the new method connectCamera, create a camera manager instance and make an openCamera method call from it

private void connectCamera() {
    CameraManager cameraManager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
    try {
        cameraManager.openCamera(mCameraId, mCameraDeviceStateCallback, mBackgroundHandler);
    } catch (CameraAccessException e) {
        e.printStackTrace();
    }
}

 Marshmallow Permissions

You will notice the requires permission warning if your application has been based on android 6.0 or later. We need to add the marshmallow runtime permissions.

Create a member for the camera request permission code

private static final int REQUEST_CAMERA_PERMISSION_RESULT = 0;

Code for the request permissions check

 private void connectCamera() {
        CameraManager cameraManager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
        try {
            if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                if(ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) ==
                        PackageManager.PERMISSION_GRANTED) {
                    cameraManager.openCamera(mCameraId, mCameraDeviceStateCallback, mBackgroundHandler);
                } else {
                    if(shouldShowRequestPermissionRationale(Manifest.permission.CAMERA)) {
                        Toast.makeText(this,
                                "Video app required access to camera", Toast.LENGTH_SHORT).show();
                    }
                    requestPermissions(new String[] {Manifest.permission.CAMERA}, REQUEST_CAMERA_PERMISSION_RESULT);
                }

            } else {
                cameraManager.openCamera(mCameraId, mCameraDeviceStateCallback, mBackgroundHandler);
            }
        } catch (CameraAccessException e) {
            e.printStackTrace();
        }
    }

 Code for the permissions result response

 @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if(requestCode == REQUEST_CAMERA_PERMISSION_RESULT) {
            if(grantResults[0] != PackageManager.PERMISSION_GRANTED) {
                Toast.makeText(getApplicationContext(),
                        "Application will not run without camera services", Toast.LENGTH_SHORT).show();
            }
        }
    }

Call the connectCamera method

From inside onResume

@Override
    protected void onResume() {
        super.onResume();

        startBackgroundThread();

        if(mTextureView.isAvailable()) {
            setupCamera(mTextureView.getWidth(), mTextureView.getHeight());
            connectCamera();
        } else {
            mTextureView.setSurfaceTextureListener(mSurfaceTextureListener);
        }
    }

From inside the surface texture listener callback

private TextureView.SurfaceTextureListener mSurfaceTextureListener = new TextureView.SurfaceTextureListener() {
        @Override
        public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
            setupCamera(width, height);
            connectCamera();

        }

 

Android video app connect camera device summary

In the android video app camera device setup summary tutorial we learned how to create a camera device from the camera manager’s open camera api.

We also encountered the issue where we had to provide runtime permissions for access to the camera services.

About The Author
-

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>