Reputation: 11
I'm trying to get access to my camera and what happened is that it does not open the camera and kept says that "Can request only one set of permissions at a time". So anybody can help with this problem?
I got this message on my logcat:
I/Timeline: Timeline: Activity_launch_request time:318319760 intent:Intent { act=android.content.pm.action.REQUEST_PERMISSIONS pkg=com.google.android.packageinstaller (has extras) } W/Activity: Can request only one set of permissions at a time
And here is my code that I've referred for requesting the permission on camera:
private static final String[] PERMISSIONS = {
Manifest.permission.CAMERA
};
private static final int REQUEST_PERMISSIONS = 42;
private static final int PERMISSION_COUNT = 1;
@SuppressLint("NewApi")
private boolean arePermissionDenied(){
for(int i = 0; i<PERMISSION_COUNT;i++){
if(checkSelfPermission(PERMISSIONS[i])!= PackageManager.PERMISSION_GRANTED){
return true;
}
}
return false;
}
@SuppressLint("NewApi")
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults){
super.onRequestPermissionsResult(requestCode,permissions,grantResults);
if(requestCode==REQUEST_PERMISSIONS && grantResults.length > 0){
if(arePermissionDenied()){
((ActivityManager)(this.getSystemService(ACTIVITY_SERVICE))).clearApplicationUserData();
recreate();
}
else{
onResume();
}
}
else{
}
}
//Variable onResume method
private boolean isCameraInitialized;
private Camera mCamera = null;
private static SurfaceHolder myHolder;
private static CameraPreview mPreview;
private static OrientationEventListener orientationEventListener = null;
@Override
protected void onResume(){
super.onResume();
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
requestPermissions(PERMISSIONS,REQUEST_PERMISSIONS);
return;
}
if(!isCameraInitialized){
mCamera=Camera.open();
mPreview = new CameraPreview(this,mCamera);
frameLayout.addView(mPreview);
rotateCamera();
orientationEventListener = new OrientationEventListener(this) {
@Override
public void onOrientationChanged(int orientation) {
rotateCamera();
}
};
orientationEventListener.enable();
}
}
Upvotes: 1
Views: 3689
Reputation: 61
You can try below code. Firstly use something like this to check if the user has granted permission instead of your arePermissionDenied
method:
if (ContextCompat.checkSelfPermission(context, Manifest.permission.CAMERA)
== PackageManager.PERMISSION_DENIED)
Then, you could request users to grant permission as follows:
ActivityCompat.requestPermissions(activity, new String[] {Manifest.permission.CAMERA}, requestCode);
After clicking request we can get result in you onRequestPermissionResult
method as demonstrated below:
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == MY_CAMERA_REQUEST_CODE) {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(this, "camera permission granted", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(this, "camera permission denied", Toast.LENGTH_LONG).show();
}
}
}
Hope this helps!!!
Upvotes: 1