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

前端 未结 6 1582
遇见更好的自我
遇见更好的自我 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

    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) {
            }
        }
    } 
    

提交回复
热议问题