我已在我的应用程序中集成了ZXing条形码扫描程序库。我正在处理Android 6.0设备的请求权限对话框。但我的问题是当我在请求权限对话框中选择“拒绝”时收到如下图像的错误如果我点击“允许”应用程序工作正常。任何人都可以帮助我。我很长时间都在摸不着头脑来解决这个问题。谢谢。[
以下是我的代码片段,我在其中处理Marshmallow的权限请求。
if (version >= android.os.Build.VERSION_CODES.M) {
if (ContextCompat.checkSelfPermission(context,
Manifest.permission.CAMERA)
!= PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(SearchActivity.this,
Manifest.permission.CAMERA)) {
ActivityCompat.requestPermissions(SearchActivity.this,
new String[]{Manifest.permission.CAMERA},
MY_PERMISSIONS_REQUEST_CAMERA);
} else {
// No explanation needed, we can request the permission.
ActivityCompat.requestPermissions(SearchActivity.this,
new String[]{Manifest.permission.CAMERA},
MY_PERMISSIONS_REQUEST_CAMERA);
}
}
}
@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case MY_PERMISSIONS_REQUEST_CAMERA: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
int version3 = Build.VERSION.SDK_INT;
// permission was granted, yay! Do the
// contacts-related task you need to do.
Toast.makeText(SearchActivity.this, "Permission granted", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(SearchActivity.this, "Camera is required", Toast.LENGTH_SHORT).show();
}
return;
}
}
}
以下是我的错误日志。
08-23 10:49:07.894 9832-10612/com.test.xxx E/CameraInstance﹕ Failed to open camera
java.lang.RuntimeException: Fail to connect to camera service
at android.hardware.Camera.<init>(Camera.java:511)
at android.hardware.Camera.open(Camera.java:353)
at com.google.zxing.client.android.camera.open.OpenCameraInterface.open(OpenCameraInterface.java:83)
at com.journeyapps.barcodescanner.camera.CameraManager.open(CameraManager.java:121)
at com.journeyapps.barcodescanner.camera.CameraInstance$3.run(CameraInstance.java:158)
at android.os.Handler.handleCallback(Handler.java:746)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.os.HandlerThread.run(HandlerThread.java:61)
08-23 10:49:09.268 9832-10668/com.test.ecommercetrial E/CameraInstance﹕ Failed to configure camera
java.lang.NullPointerException: Attempt to invoke virtual method 'android.hardware.Camera$Parameters android.hardware.Camera.getParameters()' on a null object reference
at com.journeyapps.barcodescanner.camera.CameraManager.setParameters(CameraManager.java:353)
at com.journeyapps.barcodescanner.camera.CameraManager.configure(CameraManager.java:139)
at com.journeyapps.barcodescanner.camera.CameraInstance$4.run(CameraInstance.java:171)
at android.os.Handler.handleCallback(Handler.java:746)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.os.HandlerThread.run(HandlerThread.java:61)
答案 0 :(得分:0)
使用最新的marshmallow版本号升级您的gradle。希望它能解决这个问题。