I work with Camera2API and use for this the Google example which is on the gita

Mysticism is in this method which opens the camera

 private void openCamera(int width, int height) { setUpCameraOutputs(width, height); configureTransform(width, height); CameraManager manager = (CameraManager) getSystemService(Context.CAMERA_SERVICE); try { if (!mCameraOpenCloseLock.tryAcquire(2500, TimeUnit.MILLISECONDS)) { throw new RuntimeException("Time out waiting to lock camera opening."); } if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) { return; } manager.openCamera(mCameraId, mStateCallback, mBackgroundHandler); } catch (CameraAccessException e) { e.printStackTrace(); } catch (InterruptedException e) { throw new RuntimeException("Interrupted while trying to lock camera opening.", e); } } 

Now I use Samsung S6 android 6.0

It is expected that the method manager.openCamera(mCameraId, mStateCallback, mBackgroundHandler); - will open the camera, but instead this code stops at

 if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) { return; } 

as far as I understand, it does not pass the permission check, I checked it in the manifest and there is the necessary permission

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

What can this mean?

Then I tried to open the camera on the Meizu MX5 android 5.1 and everything opens and works correctly

Can the problem be in the versions of android?

What am I doing wrong? How can this be verified?

  • 2
    targetSDK=22 and try, if it helps, it means that you incorrectly request the rights on the 6 android. - pavel
  • @pavel yes for sure! Everything worked)) Cool! What to do now? Are there any standard verification methods or just targetSDK should always be 22 for all devices? - Aleksey Timoshchenko
  • there is, but it is a separate topic) habrahabr.ru/post/278945 can help, or Google) - pavel

4 answers 4

The rules for requesting permissions have changed in android 6.0

Now you need to request permissions, and not just register in manifest .

Google docs link

    As a result, everything worked this way, read the off article and there are really a lot of changes for 6 (by the way, I liked how everything is described here ), well, in general, according to all the new lessons, such a solution has gathered

    With the form of the request dialog implementation

     public void camera(View view) { toCamera(); } private void toCamera() { if (!isCheckPermission()){ return; } if (isProcessWasFinish()) { startActivity(new Intent(getApplicationContext(), CameraActivity.class)); overridePendingTransition(R.anim.open_next, R.anim.close_main); } else { startActivity(new Intent(getApplicationContext(), UserDataScreen.class)); overridePendingTransition(R.anim.open_next, R.anim.close_main); } } private boolean isCheckPermission() { if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) { // Should we show an explanation? if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.CAMERA)) { showMessageOKCancel("You need to allow access to Camera"); return false; } // No explanation needed, we can request the permission. ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.CAMERA}, MY_PERMISSIONS_REQUEST); return false; // MY_PERMISSIONS_REQUEST is an // app-defined int constant. The callback method gets the // result of the request. } return true; } private void showMessageOKCancel(String message) { new AlertDialog.Builder(MainActivity.this) .setMessage(message) .setPositiveButton("OK", listener) .setNegativeButton("Cancel", listener) .create() .show(); } DialogInterface.OnClickListener listener = new DialogInterface.OnClickListener() { final int BUTTON_NEGATIVE = -2; final int BUTTON_POSITIVE = -1; @Override public void onClick(DialogInterface dialog, int which) { switch (which) { case BUTTON_NEGATIVE: // int which = -2 dialog.dismiss(); break; case BUTTON_POSITIVE: // int which = -1 ActivityCompat.requestPermissions( MainActivity.this, new String[]{android.Manifest.permission.CAMERA}, MY_PERMISSIONS_REQUEST); dialog.dismiss(); break; } } }; @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { switch (requestCode) { case MY_PERMISSIONS_REQUEST: { // If request is cancelled, the result arrays are empty. if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { Log.e(MY_LOG, "Camera permission Granted"); // permission was granted, yay! Do the // contacts-related task you need to do. toCamera(); } else { Log.e(MY_LOG, "Camera permission Denied"); // permission denied, boo! Disable the // functionality that depends on this permission. } } default: { super.onRequestPermissionsResult(requestCode, permissions, grantResults); } // other 'case' lines to check for other // permissions this app might request } } 

      The fact that you figure it out is very good. But hands every time to check and request permission is very inconvenient. Use ready-made solutions https://github.com/Karumi/Dexter

      • Can you explain in more detail what the difference is? As far as I looked, this library does the same thing ... If there is no permission, then a dialogue is shown, if the dialogue was denied once, another dialogue is shown which says that you need to open the permissions ... I just didn’t fully understand what it means to check hands? - Aleksey Timoshchenko
      • In essence, it does the same thing, but the developer no longer needs to override onActivityResult and onRequestPermissionsResult, and make a request for a service. All this is generated automatically. Now imagine that you need several permitshins at once. This library allows you to make the code cleaner and simpler - Alex Key
      • one
        Yeah, much cleaner - ponazasorit project a few dozen files from the library: github.com/Karumi/Dexter/tree/master/dexter/src/main/java/com /..., it is not always stupid to use, sometimes it's better to stir up the handles .... - ilw
      1. http://easyrobot.online/temp/permission6.jar
      2. if (! AccessPermission.getPermmission (this)) {

          AccessPermission.setOnPermmissionResult(new OnPermmissionResult() { @Override public void OnPermmission(boolean access, String[] permissions, int[] grantResults) { 

        if (access) init (); }}); } else init ();

      3. - in the manifesto