본문 바로가기
모바일개발(Mobile Dev)/안드로이드개발(Android)

camera permission confirmation

by 테크한스 2016. 9. 3.
반응형



Request the permissions you need

If your app doesn't already have the permission it needs, the app must call one of the requestPermissions() methods to request the appropriate permissions. Your app passes the permissions it wants, and also an integer request code that you specify to identify this permission request. This method functions asynchronously: it returns right away, and after the user responds to the dialog box, the system calls the app's callback method with the results, passing the same request code that the app passed to requestPermissions().

The following code checks if the app has permission to read the user's contacts, and requests the permission if necessary:



// Here, thisActivity is the current activity
if (ContextCompat.checkSelfPermission(thisActivity,
               
Manifest.permission.READ_CONTACTS)
       
!= PackageManager.PERMISSION_GRANTED) {

   
// Should we show an explanation?
   
if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
           
Manifest.permission.READ_CONTACTS)) {

       
// Show an expanation to the user *asynchronously* -- don't block
       
// this thread waiting for the user's response! After the user
       
// sees the explanation, try again to request the permission.

   
} else {

       
// No explanation needed, we can request the permission.

       
ActivityCompat.requestPermissions(thisActivity,
               
new String[]{Manifest.permission.READ_CONTACTS},
                MY_PERMISSIONS_REQUEST_READ_CONTACTS
);

       
// MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
       
// app-defined int constant. The callback method gets the
       
// result of the request.
   
}
}

Handle the permissions request response

When your app requests permissions, the system presents a dialog box to the user. When the user responds, the system invokes your app'sonRequestPermissionsResult() method, passing it the user response. Your app has to override that method to find out whether the permission was granted. The callback is passed the same request code you passed to requestPermissions(). For example, if an app requests READ_CONTACTSaccess it might have the following callback method:


@Override
public void onRequestPermissionsResult(int requestCode,
       
String permissions[], int[] grantResults) {
   
switch (requestCode) {
       
case MY_PERMISSIONS_REQUEST_READ_CONTACTS: {
           
// If request is cancelled, the result arrays are empty.
           
if (grantResults.length > 0
               
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {

               
// permission was granted, yay! Do the
               
// contacts-related task you need to do.

           
} else {

               
// permission denied, boo! Disable the
               
// functionality that depends on this permission.
           
}
           
return;
       
}

       
// other 'case' lines to check for other
       
// permissions this app might request
   
}
}






        // Check permission for CAMERA
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.CAMERA)
!= PackageManager.PERMISSION_GRANTED) {
// Check Permissions Now
// Callback onRequestPermissionsResult interceptado na Activity MainActivity
ActivityCompat.requestPermissions(this,
new String[]{android.Manifest.permission.CAMERA},
MainActivity.REQUEST_CAMERA);
} else {
// permission has been granted, continue as usual

Intent captureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(captureIntent, 0);
}
////////////////////////////////////////////////////////////////////////////////
// Check permission for CAMERA


    ////////////////////////////////////////////////////////////////////////////////
// Check permission for CAMERA
@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {


switch (requestCode) {

case REQUEST_CAMERA: {

// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// permission was granted, yay! Do the
// contacts-related task you need to do.
Toast.makeText(getApplicationContext(), "Permission granted", Toast.LENGTH_SHORT).show();
} else {
// permission denied, boo! Disable the
// functionality that depends on this permission.
Toast.makeText(getApplicationContext(), "Permission denied", Toast.LENGTH_SHORT).show();

}
return;
}

// other 'case' lines to check for other
// permissions this app might request
}
}
////////////////////////////////////////////////////////////////////////////////



To ban a crash 


int permissionCheck = ContextCompat.checkSelfPermission(this, android.Manifest.permission.CAMERA);

if(permissionCheck== PackageManager.PERMISSION_DENIED){
// 권한 없음
Toast.makeText(getApplicationContext(), "No Permission to Access", Toast.LENGTH_SHORT).show();
}else{


반응형