How get permission for camera in android.(Specifically Marshmallow)

前端 未结 6 1561
遇见更好的自我
遇见更好的自我 2020-11-30 01:53

Hey I am designing an app in android studio. In which i require permission of camera. I have included

相关标签:
6条回答
  • 2020-11-30 02:12
    check using this
    if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.CAMERA)
                == PackageManager.PERMISSION_DENIED)
    

    and

    0 讨论(0)
  • 2020-11-30 02:12

    click here for full source code and learn more.

    Before get permission you can check api version,

      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
      {
          if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
    
          } 
          else
          {
             ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 401);
          }
      }
      else
      {
        // if version is below m then write code here,          
      }
    

    Get the Result of the permission dialog,

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == 401) {
            if (grantResults.length == 0 || grantResults == null) {
    
            } else if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                openGallery();
            } else if (grantResults[0] == PackageManager.PERMISSION_DENIED) {
            }
        } else if (requestCode == 402) {
            if (grantResults.length == 0 || grantResults == null) {
    
            } else if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
    
            } else if (grantResults[0] == PackageManager.PERMISSION_DENIED) {
            }
        }
    } 
    
    0 讨论(0)
  • 2020-11-30 02:20

    First check if the user has granted the permission:

    if (ContextCompat.checkSelfPermission(context, Manifest.permission.CAMERA)
        == PackageManager.PERMISSION_DENIED)
    

    Then, you could use this to request to the user:

    ActivityCompat.requestPermissions(activity, new String[] {Manifest.permission.CAMERA}, requestCode);
    

    And in Marshmallow, it will appear in a dialog

    0 讨论(0)
  • 2020-11-30 02:28

    Requesting Permissions In the following code, we will ask for camera permission:

    in java

    EasyPermissions is a wrapper library to simplify basic system permissions logic when targeting Android M or higher.

    Installation EasyPermissions is installed by adding the following dependency to your build.gradle file:

    dependencies {
    
        // For developers using AndroidX in their applications
        implementation 'pub.devrel:easypermissions:3.0.0'
    
        // For developers using the Android Support Library
        implementation 'pub.devrel:easypermissions:2.0.1'
    
    }
    
    
    private void askAboutCamera(){
    
            EasyPermissions.requestPermissions(
                    this,
                    "A partir deste ponto a permissão de câmera é necessária.",
                    CAMERA_REQUEST_CODE,
                    Manifest.permission.CAMERA );
    }
    
    0 讨论(0)
  • 2020-11-30 02:33

    This works for me, the source is here

    int MY_PERMISSIONS_REQUEST_CAMERA=0;
    // Here, this is the current activity
    if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED)
    {
         if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.CAMERA))
         {
    
         }
         else
         {
              ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.CAMERA}, MY_PERMISSIONS_REQUEST_CAMERA );
              // MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
              // app-defined int constant. The callback method gets the
              // result of the request.
          }
    }
    
    0 讨论(0)
  • 2020-11-30 02:37

    You can try the following code to request camera permission in marshmallow. First check if the user grant the permission. If user has not granted the permission, then request the camera permission:

    private static final int MY_CAMERA_REQUEST_CODE = 100;
    
    if (checkSelfPermission(Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
        requestPermissions(new String[]{Manifest.permission.CAMERA}, MY_CAMERA_REQUEST_CODE);
    }
    

    After requesting the permission, dialog will display to ask permission containing allow and deny options. After clicking action we can get result of the request with the following method:

    @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();
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题