Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Receiving CameraAccessException: CAMERA_ERROR (3) on CaptureSession.setRepeatingRequest()

I have the Following code running from the onCreate() method. outside of this code is just a lot of object declarations and instantiations for the callbacks.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    thisAct = this;
    display = (TextureView)findViewById(R.id.display);
    display.setSurfaceTextureListener(surfaceTextureListener);

    db = openOrCreateDatabase("MyDatabase", Context.MODE_PRIVATE, null);
    db.execSQL("CREATE TABLE IF NOT EXISTS Photos (ID INTEGER, location TEXT, size INTEGER)");
    db.execSQL("CREATE TABLE IF NOT EXISTS Tags (ID INTEGER, tag TEXT)");

    camMan = this.getSystemService(CameraManager.class);

    new Thread(new Runnable(){
        @Override
        public void run() {
            try {
                String cm = camMan.getCameraIdList()[0];

                if ( ContextCompat.checkSelfPermission(thisAct, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED ) {
                    ActivityCompat.requestPermissions((Activity)thisAct, new String[] { Manifest.permission.CAMERA }, CAMERA_REQUEST);
                }
                camMan.openCamera(cm, CDstateCallback, handle);

                while(texture == null){
                    try{
                        Thread.sleep(100);
                        Log.v("DEBUG!!!!!!!!", "Thread sleeping b/c texture is null");
                    } catch(Exception e){
                        e.printStackTrace();
                    }
                }

//              texture.setDefaultBufferSize(325,325);
                Surface s = new Surface(texture);
                surfaceList.add(s);

                while(cameraDevice == null){
                    try{
                        Thread.sleep(100);
                        Log.v("DEBUG!!!!!!!!", "Thread sleeping b/c cameraDevice is null");
                    } catch(Exception e){
                        e.printStackTrace();
                    }
                }

               cameraDevice.createCaptureSession(surfaceList, CCSstateCallback, handle);

               while(capSess == null){
                    try{
                        Thread.sleep(100);
                        Log.v("DEBUG!!!!!!!!", "Thread sleeping b/c capSess is null");
                    } catch(Exception e){
                        e.printStackTrace();
                    }
                }

                capSess.prepare(surfaceList.get(0));

                capReq = cameraDevice.createCaptureRequest(cameraDevice.TEMPLATE_PREVIEW);
                capReq.addTarget(surfaceList.get(0));
                capSess.setRepeatingRequest(capReq.build(), CCSlistener, handle);

            } catch (CameraAccessException e) {
                e.printStackTrace();
            }
        }
    }).start();
}

When this line executes, I receive the following error in the console:

capSess.setRepeatingRequest(capReq.build(), CCSlistener, handle);

.

android.hardware.camera2.CameraAccessException: CAMERA_ERROR (3): submitRequestList:257: Camera 0:  Got error Invalid argument (-22) after trying to set streaming request

After doing some searching online, it seems that this is referring to the "ERROR_CAMERA_DISABLED" code, indicating that the camera device could not be opened due to a device policy.

This initially made me think to look straight to the permissions, but as seen in the lines:

if ( ContextCompat.checkSelfPermission(thisAct, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED ) {
                        ActivityCompat.requestPermissions((Activity)thisAct, new String[] { Manifest.permission.CAMERA }, CAMERA_REQUEST);
                    }

,

I check for permissions and prompt the user for them if the app doesn't already have them. I've also checked in my app settings in the manifest file, and on the device to confirm that I do in fact have the camera hardware permissions. Can any one help me solve this issue?

like image 766
CZYK Avatar asked Nov 07 '17 01:11

CZYK


2 Answers

Adding this code to take care of API less than 25 solved the issue for me.

if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.N) {
                mCaptureSession.stopRepeating();
                mCaptureSession.abortCaptures();
}
mCaptureSession.capture(captureBuilder.build(), CaptureCallback, null);
like image 162
Ant小波 Avatar answered Nov 15 '22 02:11

Ant小波


Running the Camera Basic Sample on a Huawei tablet, I ran into this issue. The above answer works too. Just commenting the mCaptureSession.abortCaptures(); works also.

if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.N) {
    mCaptureSession.stopRepeating();
    mCaptureSession.abortCaptures();
}

Check out this github issue for more details.

Also adding sleep(100); (import from SystemClock.sleep) after mCaptureSession.abortCaptures(); also seems to be fixing the issue. (as @CatalinM commented)

like image 3
Monster Brain Avatar answered Nov 15 '22 02:11

Monster Brain