简体   繁体   中英

Checking camera permissions returns null object reference

I'm currently trying to activate the camera of the phone and before activating I am checking the permissions of the camera to see if it is allowed. However, for some reason when checking the permissions, I will get the error as shown below :

Attempt to invoke virtual method 'int android.content.Context.checkPermission(java.lang.String, int, 
int)' on a null object reference

I've tried adding to the manifest

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

and still it doesn't seem to work.

I've also increased the appcompat level to 23.1.1 and it too doesn't work.

The snippet codes of my program are shown below:

public abstract class CameraFragment extends Fragment implements CameraCallbacks {

   private CameraPreview mCameraPreview;

   public void startCamera(CameraConfig cameraConfig) {
          if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.CAMERA)
            != PackageManager.PERMISSION_GRANTED) { //check if the camera permission is available

          }else{ (mCameraPreview == null) mCameraPreview = addPreView();
            mCameraPreview.startCameraInternal(cameraConfig);
 }}}

Below are the snippet codes for CameraPreview :

class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {
   private CameraCallbacks mHiddenCameraActivity;
   CameraPreview(@NonNull Context context, CameraCallbacks cameraCallbacks) {
      super(context);

      mHiddenCameraActivity = cameraCallbacks;

      //Set surface holder
      mHolder = getHolder();
      mHolder.addCallback(this);
      mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
 }

Below are the code snippets for CameraCallback:

interface CameraCallbacks {

   void onImageCapture(@NonNull File imageFile);

   void onCameraError(@CameraError.CameraErrorCodes int errorCode);
}

Did anyone get the solution to this problem?

I've spent countless hours working on this and searching for solutions online and it still doesn't seem to work.

Thanks for the help.

You can check like this -

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
  {
      if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission. CAMERA) == PackageManager.PERMISSION_GRANTED) {
      //  you can access camera   
      } 
      else
      {
         ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission. CAMERA}, 411);
      }
  }
  else
  {
    //  you can access camera          
  }

Get Result of the Permission dialog,

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    if (requestCode == 411) {
        if (grantResults.length == 0 || grantResults == null) {
             // show dialog that you need access to go ahead
        } else if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            // Your code here permission granted
        } else if (grantResults[0] == PackageManager.PERMISSION_DENIED) {
             // show dialog that you need access to go ahead
        }
    }
} 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM