检查相机权限 returns 空对象引用
Checking camera permissions returns null object reference
我目前正在尝试激活 phone 的摄像头,在激活之前,我正在检查摄像头的权限以查看是否允许。但是,由于某些原因,在检查权限时,我会得到如下所示的错误:
Attempt to invoke virtual method 'int android.content.Context.checkPermission(java.lang.String, int,
int)' on a null object reference
我已经尝试添加到清单中
<uses-permission android:name="android.permission.CAMERA"/>
但它似乎仍然不起作用。
我也将 appcompat
级别提高到 23.1.1,但它也不起作用。
我的程序片段代码如下:
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);
}}}
以下是 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);
}
}
以下是 CameraCallback 的代码片段:
interface CameraCallbacks {
void onImageCapture(@NonNull File imageFile);
void onCameraError(@CameraError.CameraErrorCodes int errorCode);
}
有人解决了这个问题吗?
我为此花费了无数时间并在网上搜索解决方案,但似乎仍然没有用。
感谢您的帮助。
你可以这样检查 -
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
}
获取权限对话框的结果,
@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
}
}
}
我目前正在尝试激活 phone 的摄像头,在激活之前,我正在检查摄像头的权限以查看是否允许。但是,由于某些原因,在检查权限时,我会得到如下所示的错误:
Attempt to invoke virtual method 'int android.content.Context.checkPermission(java.lang.String, int,
int)' on a null object reference
我已经尝试添加到清单中
<uses-permission android:name="android.permission.CAMERA"/>
但它似乎仍然不起作用。
我也将 appcompat
级别提高到 23.1.1,但它也不起作用。
我的程序片段代码如下:
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);
}}}
以下是 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);
}
}
以下是 CameraCallback 的代码片段:
interface CameraCallbacks {
void onImageCapture(@NonNull File imageFile);
void onCameraError(@CameraError.CameraErrorCodes int errorCode);
}
有人解决了这个问题吗?
我为此花费了无数时间并在网上搜索解决方案,但似乎仍然没有用。
感谢您的帮助。
你可以这样检查 -
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
}
获取权限对话框的结果,
@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
}
}
}